我想使用手机本身的后退按钮来显示一个对话框,询问我是否确定要退出?
public void onBackPressed() {
// TODO Auto-generated method stub
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Confirm");
dialog.setMessage("Are you sure you want to exit?");
return;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText text=(EditText)findViewById(R.id.Text);
TextWatcher watch=new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
onBackPressed();
}
};
text.addTextChangedListener(watch);
}