标签:
作者:夏至 欢迎转载,也请保留这段申明,谢谢
<Button
android:id="@+id/btncancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="退出"
/>
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aty_alterdialog);
Button button = (Button)findViewById(R.id.cancel);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.cancel:
AlertDialog.Builder dialog = new AlertDialog.Builder(this); //step 1
dialog.setTitle("系统提示"); // step 2
dialog.setMessage("确定要退出吗"); // step 3
dialog.setCancelable(false);
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { // step 4
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了确定", Toast.LENGTH_SHORT).show();
finish();
}
}).create(); // step 5 这里creat() 用不用都可以,默认是用上的。
dialog.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择了中立",Toast.LENGTH_SHORT).show();
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了取消", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
break;
}
}
}
标签:
原文地址:http://www.cnblogs.com/shaorui/p/5206643.html