Java 如何在edittext-ContextChanged的情况下显示AlertDialog
我有一个Java 如何在edittext-ContextChanged的情况下显示AlertDialog,java,android,android-alertdialog,textwatcher,Java,Android,Android Alertdialog,Textwatcher,我有一个活动,有几个编辑文本。如果用户单击“Cancel”按钮,并且这些editText中没有任何更改,则应用程序应转到上一个活动,但如果这些editText中有更改,则我希望用户看到警报对话框: Save changes you made? NO YES 我已经为这些editText设置了TextWatcher,例如: //let's set up a textwatcher so if the state of any of the edittexts has chang
活动
,有几个编辑文本
。如果用户单击“Cancel
”按钮,并且这些editText
中没有任何更改,则应用程序应转到上一个活动,但如果这些editText
中有更改,则我希望用户看到警报对话框
:
Save changes you made?
NO YES
我已经为这些editText
设置了TextWatcher
,例如:
//let's set up a textwatcher so if the state of any of the edittexts has changed.
//if it has changed and user clicks 'CANCEL', we'll ask first, '
//You've made changes here. Sure you want to cancel?'
TextWatcher edittw = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Toast.makeText(EditContact.this, "change detected", Toast.LENGTH_SHORT).show();
}
};
categoryname.addTextChangedListener(edittw);
namename.addTextChangedListener(edittw);
phonename.addTextChangedListener(edittw);
addressname.addTextChangedListener(edittw);
commentname.addTextChangedListener(edittw);
以及我的警报对话框
,用于取消
按钮-无论任何编辑文本
是否已更改,它都会出现,但我只希望它仅在编辑文本
中进行更改时才会出现,否则,不应该有AlerDialog
,当前活动应该返回到以前的活动-如下所示:
private void cancelButton() {
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//add a dialogue box
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setMessage("Save changes you made?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
});
}
//Are you sure you want to cancel? dialogue
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
pDialog = new ProgressDialog(EditContact.this);
// Showing progress dialog for the review being saved
pDialog.setMessage("Saving...");
pDialog.show();
//post the review_id in the current activity to EditContact.php and
StringRequest stringRequest = new StringRequest(Request.Method.POST, EditContact_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//hide the dialogue saying 'Saving...' when page is saved
pDialog.dismiss();
Toast.makeText(EditContact.this, response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(EditContact.this, "problem here", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
//we are posting review_id into our EditContact.php file,
//the second value, review_id,
// is the value we get from Android.
// When we see this in our php, $_POST["review_id"],
//put in the value from Android
params.put("review_id", review_id);
return params;
}
};
AppController.getInstance().addToRequestQueue(stringRequest);
//when cancelled, back to the PopulistoListView class
Intent j = new Intent(EditContact.this,PopulistoListView.class);
startActivity(j);
break;
case DialogInterface.BUTTON_NEGATIVE:
//close the activity
finish();
}
}
};
private void cancelButton(){
cancel.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//添加一个对话框
AlertDialog.Builder=新建AlertDialog.Builder(view.getContext());
setMessage(“保存所做的更改?”)。setPositiveButton(“是”,对话框ClickListener)
.setNegativeButton(“否”,对话框ClickListener).show();
}
});
}
//你确定要取消吗?对话
DialogInterface.OnClickListener dialogClickListener=新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
开关(哪个){
case DialogInterface.BUTTON_阳性:
//单击“是”按钮
pDialog=newprogressdialog(EditContact.this);
//显示正在保存的审阅的进度对话框
pDialog.setMessage(“保存…”);
pDialog.show();
//将当前活动中的review_id发布到EditContact.php并
StringRequest StringRequest=新的StringRequest(Request.Method.POST,EditContact\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//保存页面时隐藏“正在保存…”对话框
pDialog.disclose();
Toast.makeText(EditContact.this,response,Toast.LENGTH_LONG.show();
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(EditContact.this,“这里有问题”,Toast.LENGTH_LONG.show();
}
}) {
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
//我们正在将review_id发布到EditContact.php文件中,
//第二个值,review_id,
//是我们从Android获得的价值。
//当我们在php中看到这一点时,$\u POST[“review\u id”],
//输入来自Android的值
参数put(“审查id”,审查id);
返回参数;
}
};
AppController.getInstance().addToRequestQueue(stringRequest);
//取消后,返回到PopulatoListView类
Intent j=新的Intent(EditContact.this,PopulistoListView.class);
星触觉(j);
打破
案例对话框界面按钮\否:
//关闭活动
完成();
}
}
};
我在互联网上搜索了一些教程或帖子,其中有“TextWatcher”和“AlertDialog”之类的短语,但我没有找到任何可以帮助我实现目标的内容。尝试添加AlertDialog,如下图所示,您可以将对话框放在AfterChange方法中:
TextWatcher edittw=new TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
@凌驾
公共无效后文本已更改(可编辑){
Toast.makeText(EditContact.this,“检测到更改”,Toast.LENGTH_SHORT.show();
AlertDialog.Builder;
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.LOLLIPOP){
builder=newalertdialog.builder(getApplicationContext(),android.R.style.Theme\u Material\u Dialog\u Alert);
}否则{
builder=新建AlertDialog.builder(getApplicationContext());
}
builder.setTitle(“删除条目”)
.setMessage(“确实要删除此条目吗?”)
.setPositiveButton(android.R.string.yes,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
//你想要是吗
}
})
.setNegativeButton(android.R.string.no,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
//你不想要吗
}
})
.setIcon(android.R
//onCancel Clicked
if(somethingChanged)//compare old values with new values
showDialog();
else
onBackPressed();
boolean isDirty;
TextWatcher edittw = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Toast.makeText(EditContact.this, "change detected", Toast.LENGTH_SHORT).show();
isDirty = true;
}
};
categoryname.addTextChangedListener(edittw);
namename.addTextChangedListener(edittw);
phonename.addTextChangedListener(edittw);
addressname.addTextChangedListener(edittw);
commentname.addTextChangedListener(edittw);
private void cancelButton() {
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isDirty) {
//add a dialogue box
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setMessage("Save changes you made?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
else {
// this will finish the current activity and the last activity will be popped from the stack.
finish();
}
}
});
}
@Override
onClick(View v) {
if(!(categoryname.getText().toString().isEmpty() && namename.getText().toString().isEmpty() && phonename.getText().toString().isEmpty() && addressname.getText().toString().isEmpty() && commentname.getText().toString().isEmpty())) {
// show your dialog
}
else {
// normal cancel
}
private void cancelButton() {
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(changed){
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setMessage("Save changes you made?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}else {
Log.v(TAG,"nothing changed ");
}
}
});
}