如何使用edittext onTextChanged

时间:2018-03-19 16:27:38

标签: java android android-alertdialog textwatcher

我有activityEditTexts。如果用户点击“Cancel”按钮并且这些EditTexts中没有任何更改,那么该应用应该转到上一个活动,但如果这些EditTexts中的内容发生了变化,那么我希望用户请参阅AlertDialog

Save changes you made?

NO       YES

我为这些TextWatcher设置了EditTexts,如:

  //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);

AlertDialog按钮的Cancel - 无论是否有任何EditTexts出现,都会出现,但我只是希望它出现在EditTexts,否则应该没有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();
            }
        }
    };

我在互联网上搜索过有关“TextWatcher”和“AlertDialog”等短语的教程或帖子,但我找不到能帮助我实现我想要做的事情。

5 个答案:

答案 0 :(得分:1)

尝试添加alertDialog,如下所示,您可以将对话框放在onAfterChange方法中:

&#13;
&#13;
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();

                AlertDialog.Builder builder;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    builder = new AlertDialog.Builder(getApplicationContext(), android.R.style.Theme_Material_Dialog_Alert);
                } else {
                    builder = new AlertDialog.Builder(getApplicationContext());
                }
                builder.setTitle("Delete entry")
                        .setMessage("Are you sure you want to delete this entry?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                //You want yes
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                //You want no
                            }
                        })
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();
            }
        };
&#13;
&#13;
&#13;

答案 1 :(得分:1)

创建boolean变量以跟踪textChange

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();
                }

            }

        });


    }

答案 2 :(得分:1)

你只需使用类似的布尔变量

  • 在类属性中声明检查变量

    布尔检查;

  • onTextChange()

  • 中将此变量的值设置为trueTextWatcher
  • cancelButton()更改为此

    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 ");
    
            }
        }
    });
    
    }
    

答案 3 :(得分:0)

你可以做这样的事情

OrbitControls

答案 4 :(得分:0)

在取消按钮onClick方法中,试试这个: -

 @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

   }

所有check the text中只有edittexts,如果其中任何一个不为空,则显示对话框.. !!

  

无需为上述问题添加textChangeListener !!

相关问题