更改与显示文字标签TextView标签的使用

本文出自 “Android_Tutor” 博客

转载地址:http://weizhulin.blog.51cto.com/1556324/311488

在HelloAndroid 基础之上,进行与TextView 文字标签的第一次接触.在此例中,将会在Layout 中创建TextView 对象,并学会定义res/values/string.xml 里的字符串常数,最后通过TextView 的setText 方法,在预加载程序之初,更改TextView 文字.

首先看一下运行结果如下图:

 

650) this.width=650;" onsubmit onchange onunload onselect onreset>

首先"欢迎来到魏祝林的博客"这几个字是从什么地方来的呢,我们是在res->values->string.xml里面加了如下一句(黑体):

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="hello">Hello World, HelloAndroid!</string> 
  4.     <string name="app_name">HelloAndroid</string> 
  5.     <string name="textView_text">欢迎来到魏祝林的博客</string>   
  6. </resources> 
  7.  

而加载"欢迎来到魏祝林的博客"是在main.xml (定义手机布局界面的)里加入的,如下面代码,其中我们闺将@string/hello 改成了@string/textView_text .

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView   
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/textView_text"   
  11.     /> 
  12. </LinearLayout> 
  13.  

这样我们运行HelloAndroid.java时,手机画面里将显示"欢迎来到魏祝林的博客"的欢迎界面,貌似我们又是没有写代码,只是在.xml加了一两行搞定,对习惯了编程的同学,感觉有点不适应.其实在HelloAndroid.java写代码也可以完全达到一样的效果.

在这里我们首先将main.xml回归到原样在原样的基础上加上一行见下方(黑体行)这里ID是为了在Java类里,找到TextView对象,并且可以控制它:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView   
  8.  
  9.     android:id="@+id/myTextView"   
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"   
  12.     android:text="@string/hello" 
  13.     /> 
  14. </LinearLayout> 
  15.  

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

 

 

 
 
  1.    
  2. package com.android.test;    
  3. import android.app.Activity;    
  4. import android.os.Bundle;    
  5. import android.widget.TextView;     
  6.    
  7. public class HelloAndroid extends Activity {    
  8.         
  9.     private TextView myTextView;    
  10.     public void onCreate(Bundle savedInstanceState) {    
  11.         super.onCreate(savedInstanceState);    
  12.         //载入main.xml Layout,此时myTextView:text为hello    
  13.         setContentView(R.layout.main);    
  14.             
  15.         //使用findViewById函数,利用ID找到该TextView对象    
  16.         myTextView = (TextView)findViewById(R.id.myTextView);     
  17.         String welcome_mes = "欢迎来到魏祝林的博客";     
  18.    
  19.    
  20.         //利用setText方法将TextView文字改变为welcom_mes    
  21.         myTextView.setText(welcome_mes);     
  22.     }    
  23. }    
  24.    

两种方法都可以达到一样的效果,不过我在此建议用第一种比较规范一点.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值