sunwei07

          Android入門 新手學(xué)習(xí) 開發(fā)

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            4 Posts :: 1 Stories :: 0 Comments :: 0 Trackbacks

          首先我們是在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 (定義手機(jī)布局界面的)里加入的,如下面代碼,其中我們閨將@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>  

          這樣我們運(yùn)行HelloAndroid.java時(shí),手機(jī)畫面里將顯示"歡迎來到博客"的歡迎界面,貌似我們又是沒有寫代碼,只是在.xml加了一兩行搞定,對(duì)習(xí)慣了編程的同學(xué),感覺有點(diǎn)不適應(yīng).其實(shí)在HelloAndroid.java寫代碼也可以完全達(dá)到一樣的效果.

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

          <?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,此時(shí)myTextView:text為hello            

          setContentView(R.layout.main);                        //使用findViewById函數(shù),利用ID找到該TextView對(duì)象           

           myTextView = (TextView)findViewById(R.id.myTextView);             

          String welcome_mes = "歡迎來到博客";                   //利用setText方法將TextView文字改變?yōu)閣elcom_mes           

           myTextView.setText(welcome_mes);        

           }   

           }    


          posted on 2011-02-15 10:02 sunwei_07 閱讀(348) 評(píng)論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 宜黄县| 陇南市| 溆浦县| 历史| 元阳县| 嘉禾县| 宁陕县| 安远县| 浦县| 都江堰市| 普兰店市| 肥东县| 毕节市| 金塔县| 三台县| 洛南县| 长沙市| 扬州市| 南召县| 卓资县| 扎赉特旗| 商南县| 宁津县| 土默特左旗| 岢岚县| 兰州市| 绍兴市| 丘北县| 安义县| 富顺县| 天气| 灌南县| 上思县| 镇远县| 洛南县| 墨玉县| 台中市| 且末县| 城固县| 大关县| 娱乐|