sunwei07

          Android入門 新手學習 開發

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            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 (定義手機布局界面的)里加入的,如下面代碼,其中我們閨將@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);        

           }   

           }    


          posted on 2011-02-15 10:02 sunwei_07 閱讀(346) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 肥城市| 三门县| 天台县| 襄城县| 鄂托克旗| 犍为县| 定南县| 大冶市| 商水县| 万盛区| 新巴尔虎左旗| 扎鲁特旗| 鄂托克前旗| 琼海市| 津市市| 尉氏县| 河间市| 轮台县| 延长县| 即墨市| 襄樊市| 彰化县| 澄迈县| 藁城市| 什邡市| 怀柔区| 宁乡县| 江孜县| 汾西县| 于田县| 绥棱县| 景谷| 平乐县| 磐石市| 商丘市| 腾冲县| 贵南县| 永定县| 枞阳县| 苍南县| 辽阳市|