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 });
运行效果:
posted @ 2017-04-08 12:30  AndrioidCSY  阅读(11578)  评论(0编辑  收藏  举报