日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Android基礎教程(一)之------更改與顯示文字標

系統 2360 0

在Android初級教程(五)我們寫了 HelloAndroid 之后,一直覺得沒有寫半行代碼對不起自己,所以本節,我們將在 HelloAndroid 基礎之上,進行與 TextView 文字標簽的第一次接觸.在此例中,將會在 Layout 中創建 TextView 對象,并學會定義 res/values/string.xml 里的字符串常數,最后通過 TextView setText 方法,在預加載程序之初,更改 TextView 文字.

首先看一下運行結果如下圖:

Android基礎教程(一)之------更改與顯示文字標簽TextView標簽的使用

首先"歡迎來到魏祝林的博客"這幾個字是從什么地方來的呢,我們是在res->values->string.xml里面加了如下一句(黑體):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid!</string>
<string name="app_name">HelloAndroid</string>
<string name="textView_text">歡迎來到魏祝林的博客</string>
</resources>

而加載"歡迎來到魏祝林的博客"是在 main.xml (定義手機布局界面的)里加入的,如下面代碼,其中我們閨將 @string/hello 改成了 @string/textView_text .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/textView_text"
/>
</LinearLayout>

這樣我們運行HelloAndroid.java時,手機畫面里將顯示"歡迎來到魏祝林的博客"的歡迎界面,貌似我們又是沒有寫代碼,只是在.xml加了一兩行搞定,對習慣了編程的同學,感覺有點不適應.其實在HelloAndroid.java寫代碼也可以完全達到一樣的效果.

在這里我們首先將main.xml回歸到原樣在原樣的基礎上加上一行見下方(黑體行)這里ID是為了在Java類里,找到TextView對象,并且可以控制它:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView

android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

在主程序HelloAndroid.java里代碼如下:

package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {

private TextView myTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//載入main.xml Layout,此時myTextView:text為hello
setContentView(R.layout.main);

//使用findViewById函數,利用ID找到該TextView對象
myTextView = (TextView)findViewById(R.id.myTextView);
String welcome_mes = "歡迎來到魏祝林的博客";


//利用setText方法將TextView文字改變為welcom_mes
myTextView.setText(welcome_mes);
}
}

兩種方法都可以達到一樣的效果,不過我在此建議用第一種比較規范一點.這一節就到此為至!!下一節我們將講一下Android五大布局.希望大家繼續關注~

Android基礎教程(一)之------更改與顯示文字標簽TextView標簽的使用


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 和龙市| 丰都县| 朝阳县| 晋宁县| 界首市| 湖南省| 大冶市| 井冈山市| 沅陵县| 稻城县| 昌图县| 绿春县| 双流县| 临江市| 江孜县| 竹溪县| 康定县| 分宜县| 玉屏| 高阳县| 阳西县| 墨竹工卡县| 贵溪市| 县级市| 左云县| 常熟市| 泸州市| 林周县| 广宁县| 县级市| 梅州市| 博罗县| 苍南县| 新绛县| 玛沁县| 仁化县| 涡阳县| 普兰县| 台山市| 新疆| 彭山县|