Android简单实现对话框

网络111 刘修军

简单实现对话框,界面如下:

代码:

public class MyDialogActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button1=(Button) findViewById(R.id.button1);
        this.registerForContextMenu(button1);//注册菜单
        button1.setOnClickListener(this);
    }

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
		alertdialog.setTitle("提示");
		alertdialog.setMessage("你真的要退出吗?");
		alertdialog.setPositiveButton("是", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				setContentView(R.layout.login); // 跳转到Login界面
			}
		})
		.setNegativeButton("否",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
				
			}
		});
		
		alertdialog.show();
		
		
	}
}

  

原文地址:https://www.cnblogs.com/LXJ416/p/3018506.html