首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

button2-多个旋钮的setOnClickListener

2012-10-05 
button2---多个按钮的setOnClickListenerpublic class Test extends Activity {? ? /** Called when the a

button2---多个按钮的setOnClickListener

public class Test extends Activity {

? ? /** Called when the activity is first created. */

? ? private Button.OnClickListener button_listener = new Button.OnClickListener() {

? ? ? ? public void onClick(View v) {

? ? ? ? ? ? showDialog(1);

? ? ? ? }

? ? };

?

? ? private Button.OnClickListener listener2 = new Button.OnClickListener() {

?

? ? ? ? @Override

? ? ? ? public void onClick(View v) {

? ? ? ? ? ? // TODO Auto-generated method stub

? ? ? ? ? ? showDialog(2);

? ? ? ? }

? ? };

?

? ? private Button.OnClickListener listener3 = new Button.OnClickListener() {

?

? ? ? ? @Override

? ? ? ? public void onClick(View v) {

? ? ? ? ? ? // TODO Auto-generated method stub

? ? ? ? ? ? showDialog(3);

? ? ? ? }

? ? };

?

? ? @Override

? ? public void onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.main);

? ? ? ? init();

? ? }

?

? ? private void init() {

? ? ? ? // TODO Auto-generated method stub

? ? ? ? Button button1 = (Button) findViewById(R.id.button1);

? ? ? ? Button button2 = (Button) findViewById(R.id.button2);

? ? ? ? Button button3 = (Button) findViewById(R.id.button3);

? ? ? ? button1.setOnClickListener(button_listener);

? ? ? ? button2.setOnClickListener(listener2);

? ? ? ? button3.setOnClickListener(listener3);

? ? }

?

? ? @Override

? ? protected Dialog onCreateDialog(int id) {

? ? ? ? switch (id) {

? ? ? ? ? ? case 1: {

?

? ? ? ? ? ? ? ? Dialog dialog = new AlertDialog.Builder(Test.this).setTitle("Question")

?

? ? ? ? ? ? ? ? .setMessage("one111111111111111111111")// 设置内容

? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("Yes", // 设置确定按钮

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setResult(RESULT_OK);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }).setNegativeButton("No", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }).create();// 创建

? ? ? ? ? ? ? ? return dialog;

? ? ? ? ? ? }

? ? ? ? ? ? case 2: {

? ? ? ? ? ? ? ? ScrollView sv = new ScrollView(this);

? ? ? ? ? ? ? ? TextView tv = new TextView(this);

? ? ? ? ? ? ? ? tv.setText("2");

? ? ? ? ? ? ? ? sv.addView(tv);

? ? ? ? ? ? ? ? Dialog dialog2 = new AlertDialog.Builder(Test.this).setTitle("Question")

?

? ? ? ? ? ? ? ? .setView(sv)// 设置内容

? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("Yes", // 设置确定按钮

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setResult(RESULT_OK);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }).setNegativeButton("No", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }).create();// 创建

? ? ? ? ? ? ? ? return dialog2;

? ? ? ? ? ? }

? ? ? ? ? ? case 3: {

? ? ? ? ? ? ? ? ScrollView sv = new ScrollView(this);

? ? ? ? ? ? ? ? TextView tv = new TextView(this);

? ? ? ? ? ? ? ? tv.setText("3");

? ? ? ? ? ? ? ? sv.addView(tv);

? ? ? ? ? ? ? ? Dialog dialog3 = new AlertDialog.Builder(Test.this).setTitle("Question")

?

? ? ? ? ? ? ? ? .setView(sv)// 设置内容

? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("Yes", // 设置确定按钮

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setResult(RESULT_OK);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }).setNegativeButton("No", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }).create();// 创建

? ? ? ? ? ? ? ? return dialog3;

? ? ? ? ? ? }

? ? ? ? ? ? default: {

? ? ? ? ? ? ? ? return null;

? ? ? ? ? ? }

? ? ? ? }

? ? }

?

}



热点排行