当前位置:编程学习 > wap >>

Android

利用一个Activity,实现三个Button的来回跳转,在退出时,点击AlertDialog对话框的取消按钮实现上一次状态的返回功能
public class Demo01 extends Activity {

private static String[] arr=new String[20];

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    Button button1=(Button)findViewById(R.id.button1) ;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
    Button button2=(Button)findViewById(R.id.button2) ;
    Button button3=(Button)findViewById(R.id.button3) ;
    
     //button1的点击事件
    button1.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

jumpmain();
}
});
    
      //button2的点击事件
     button2.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

jumpToLayout2();
}
});
     
     //button3的点击事件
    button3.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

jumpToLayout3();
}
});
    
    }
    
    public void jumpmain() {
     /* 将layout 改成button2.XML */
        setContentView(R.layout.button2);
        //String arr[0]="";
        /* 以findViewById()取得Button 对象,并添加onClickListener */    
        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
        jumpToLayout2();// 调用跳转方法jumpToLayout2()
        Toast.makeText(getApplicationContext(), "跳转到buttton2",
              Toast.LENGTH_SHORT).show();
         }
    });
    }
    
    public void jumpToLayout2() {
     /* 将layout 改成button3.XML */
     setContentView(R.layout.button3);
        
     /* 以findViewById()取得Button 对象,并添加onClickListener */
     Button button2 = (Button) findViewById(R.id.button2);
     button2.setOnClickListener(new Button.OnClickListener() {
     public void onClick(View v) {
           
     jumpToLayout3();// 调用跳转方法jumpToLayout3()
     Toast.makeText(getApplicationContext(), "跳转到buttton3",
          Toast.LENGTH_SHORT).show();
        }
     });
     }
    
    public void jumpToLayout3() {
     /* 将layout 改成button1.XML */
     setContentView(R.layout.button1);
        
     /* 以findViewById()取得Button 对象,并添加onClickListener */
     Button button3 = (Button) findViewById(R.id.button3);
     button3.setOnClickListener(new Button.OnClickListener() {
     public void onClick(View v) {
     jumpmain();// 调用跳转方法jump main()
     Toast.makeText(getApplicationContext(), "跳转到buttton1",
          Toast.LENGTH_SHORT).show();
     }
     });
        }
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
        
            new AlertDialog.Builder(this)
                   
                    .setTitle(R.string.prompt)
                    .setMessage(R.string.quit_msg)
                    .setNegativeButton(R.string.cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                }
                            })
                    .setPositiveButton(R.string.confirm,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    finish();
                                }
                            }).show();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
    //彻底退出程序
    protected void onDestroy() {
        super.onDestroy();
        System.exit(0);
        
    }
    
 }
        }
怎么在这个方法当中利用数组存储Button的点击事件 --------------------编程问答-------------------- 给每个Button设置一个Tag,用一个点击事件解决,根据Tag的值判断点击的是哪个按钮,然后做对应处理,不用数据保存。
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,