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 });