Android基礎TOP1:點擊按鈕改變TextView文本


Actyivity:

 1         <TextView
 2         android:id="@+id/t1"
 3         android:layout_width="wrap_content"
 4         android:layout_height="wrap_content"
 5         android:text="@string/hello_world" />
 6 //添加Button按鈕 設置其ID;寬高;等等
 7         <Button
 8         android:id="@+id/b1"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:layout_alignLeft="@+id/t1"
12         android:layout_below="@+id/t1"
13         android:layout_marginTop="41dp"
14         android:onClick="dj"
15         android:text="點我增長1cm" />

JAVA:

 1 protected void onCreate(Bundle savedInstanceState) {
 2         super.onCreate(savedInstanceState);
 3         setContentView(R.layout.activity_main);
 4         setContentView(R.layout.activity_main);
 5 //獲取TextView,Button控件
 6         final TextView tv=(TextView) findViewById(R.id.t1);
 7         tv.setText("現在時間---請看你的手機!!");
 8         final Button btn = (Button)findViewById(R.id.b1);
 9 //設置點擊后TextView現實的內容
10         btn.setOnClickListener(new View.OnClickListener() {
11 @Override
12 public void onClick(View v) {
13 // TODO Auto-generated method stub
14 tv.setText("想得美");
15 }
16 });
運行效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM