Android recipe 新的设置onClick监听器的方法
注意,这个方法只支持API level4以上。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onMyClick" />
</LinearLayout>
package my.demo;
import android.app.Activity;
public class TempActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onMyClick(View v) {
switch (v.getId()) {
case R.id.button:
((TextView) findViewById(R.id.text)).setText("被点击!");
break;
}
}
}