我的对话框中的Editext TextWacther无法正常工作或启动

时间:2017-05-09 05:08:54

标签: java android alertdialog

我正在尝试实施密码更新对话框,当用户点击对话框时出现EditText,AlertDialogpops up,EditTextAlertDialog内,但是某种程度上,TextWacther无效。我在这里缺少什么?

 final android.app.AlertDialog.Builder alert = new android.app.AlertDialog.Builder(Landing_page.this);
            String title="Change Password";
            alert.setCancelable(false);

            alert.setTitle(title); //Set Alert dialog title here
            alert.setIcon(R.drawable.sett);


            LinearLayout mainll = new LinearLayout(context);

            LinearLayout alertLL = new LinearLayout(context);
            alertLL.setOrientation(LinearLayout.HORIZONTAL);
            alertLL.setPadding(0, 10, 0, 10);

            final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);

            final TextView root = new TextView(context);
            root.setPadding(10, 0, 10, 0);
            root.setTextSize(14);
            root.setTextColor(getResources().getColor(R.color.black));
            root.setTypeface(tf);
            root.setText("Old Password");

            final EditText rootpat = new EditText(context);
            rootpat.setLayoutParams(params);
            rootpat.setTypeface(tff);

            LinearLayout alertLL0 = new LinearLayout(context);
            alertLL0.setPadding(0, 10, 0, 10);
            alertLL0.setOrientation(LinearLayout.HORIZONTAL);
            alertLL0.addView(root);
            alertLL0.addView(rootpat);

            // Set an EditText view to get user input
            final TextView tv1 = new TextView(context);
            tv1.setPadding(10, 0, 10, 0);
            tv1.setTextColor(getResources().getColor(R.color.black));
            tv1.setTextSize(14);
            tv1.setTypeface(tf);
            tv1.setText("New Password");

            final EditText topic = new EditText(context);
            topic.setLayoutParams(params);
            topic.setTypeface(tff);
            topic.setFilters(new InputFilter[] {new InputFilter.LengthFilter(16)});
            topic.addTextChangedListener(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) {
                    if (s.toString().length()<4&&s.toString().length()>16){
                        topic.setError("Password Should be between 8 and 16 Characters");
                        requestFocus(topic);
                    }

                }
            });

            alertLL.addView(tv1);
            alertLL.addView(topic);


            final TextView tv4 = new TextView(context);
            tv4.setPadding(10, 0, 10, 0);
            tv4.setText("Confirm Password");
            tv4.setTextColor(getResources().getColor(R.color.black));
            tv4.setTextSize(14);
            tv4.setTypeface(tf);

            final EditText sensor = new EditText(context);
            sensor.setLayoutParams(params);
            sensor.setTypeface(tff);
            sensor.setFilters(new InputFilter[] {new InputFilter.LengthFilter(16)});
            sensor.addTextChangedListener(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) {
if (s.toString().length()<4&&s.toString().length()>16){
    sensor.setError("Password Should be between 8 and 16 Characters");
    requestFocus(sensor);
}
                }
            });

            LinearLayout alertLL4 = new LinearLayout(context);
            alertLL4.setPadding(0, 10, 0, 10);
            alertLL4.setOrientation(LinearLayout.HORIZONTAL);
            alertLL4.addView(tv4);
            alertLL4.addView(sensor);

            mainll.setOrientation(LinearLayout.VERTICAL);
            mainll.addView(alertLL0);
            mainll.addView(alertLL);
            mainll.addView(alertLL4);

            alert.setView(mainll);


            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    String pass1=rootpat.getEditableText().toString();
                    String pass2=topic.getEditableText().toString();
                    String pass3=sensor.getEditableText().toString();

                    if (pass2.equalsIgnoreCase(pass3)&&pass1.length()!=0&&pass2.length()!=0&&pass3.length()!=0){
                        final JSONObject object=new JSONObject();
                        try{object.put("user_name",user_name);
                            object.put("new_password",pass2);
                            object.put("password",pass1);
                        }catch (JSONException e){e.printStackTrace();}
                        try {
                            ClientResource clientResource = new ClientResource(IP.IP+"/api/users/upddate_password");
                            clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,
                                    "admin", "admin");
                            Form form = new Form();
                            form.add("data", object.toString());

                            Representation representation = clientResource.post(form
                                    .getWebRepresentation());
                            response=representation.getText().toString();
                            System.out.println(response);
                            try {
                                JSONObject object1=new JSONObject(response);
                                String mesg= (String) object1.get("message");
                                Toast.makeText(getApplicationContext(),mesg,Toast.LENGTH_LONG).show();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        //dialog.cancel();
                    }else {
                        Toast.makeText(getApplicationContext(),"Invalid Credentials.. Try again...",Toast.LENGTH_LONG).show();
                    }

                }
            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.cancel();
                }
            });

            android.app.AlertDialog alertDialog = alert.create();
            alertDialog.show();
            alertDialog.setCancelable(false);

            return true;

请帮助.. !!

2 个答案:

答案 0 :(得分:0)

你的病情有问题,

if (s.toString().length()<4&&s.toString().length()>16){
    sensor.setError("Password Should be between 8 and 16 Characters");
    requestFocus(sensor);

尝试这个

if (s.toString().length()<4 || s.toString().length()>16){
    sensor.setError("Password Should be between 8 and 16 Characters");
    requestFocus(sensor);

答案 1 :(得分:0)

更改此行,

if (s.toString().length()<4&&s.toString().length()>16){
sensor.setError("Password Should be between 8 and 16 Characters");
requestFocus(sensor);

要,

 if ((s.toString().length()< 8) || (s.toString().length()>16)){
                topic.setError("Password Should be between 8 and 16 Characters");
               // requestFocus(topic);
            }

问题:

问题是您使用&&检查密码。如果使用&&两个条件必须为true,则只需将&&替换为||

有关详细信息,请阅读此Logical Operators in Java