一、程序執行效果
二、代碼
1. ex02.java
public class ex02 extends Activity { private Button mBtnOK; private Button mBtnExit; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBtnOK = (Button)findViewById(R.id.Button01); mBtnExit=(Button)findViewById(R.id.Button02); mBtnOK.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { setTitle("this OK button"); } }); mBtnExit.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { finish(); } }); } }
2. main.xml(layout)
<?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" > <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_ok"></Button> <Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_exit"></Button> </LinearLayout>
3. strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ex02!</string> <string name="app_name">ex02</string> <string name="button_ok">OK</string> <string name="button_exit">Exit</string> </resources>
--完--