代码之家  ›  专栏  ›  技术社区  ›  astric mobiles

如何在TextWatcher中关闭AlertDialog

  •  0
  • astric mobiles  · 技术社区  · 8 年前

    我有一个ALertDialog,允许用户输入一个pin号码。。当文本的长度与文本的长度匹配时,检查pin的长度是否正确。我正在尝试关闭/取消此对话框,当pin尝试成功时。 我试过了对话框.关闭();以及对话框取消();但都没用。

    为了清楚起见,我试图关闭if/else中“else”部分的AlertDialog。谢谢你的关注

    代码:

     AlertDialog.Builder builder;
    
    public void dialog() {
    
    builder = new AlertDialog.Builder(getContext());
    builder.setTitle("Title");
    // I'm using fragment here so I'm using getView() to provide ViewGroup
    // but you can provide here any other instance of ViewGroup from your Fragment / Activity
    View viewInflated = LayoutInflater.from(getContext()).inflate(R.layout.pword, (ViewGroup) getView(), false);
    // Set up the input
    final EditText input = (EditText) viewInflated.findViewById(R.id.input);
    // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
    builder.setView(viewInflated);
    
    
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            stopReader();
            startReader();
        }
    });
    
    builder.show();
    
    input.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
    
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
            m_Text = input.getText().toString();
    
            if (m_Text.length() == details.get(2).length()) {
                if (!input.getText().toString().equals(details.get(2))) {
                    Toast.makeText(MainActivity.mainActivity, "Incorrect pin entered!", Toast.LENGTH_SHORT).show();
                    Log.d("eLOQ", "User pin is not correct");
    
                    input.setText("");
    
                } else if (input.getText().toString().equals(details.get(2))) {
                    controller.startStageTwo();
                    Log.d("eLOQ", "User pin is correct");
    
    
                    dialog.dismiss();
                }
            }
    
    
        }
    });
    

    }

    2 回复  |  直到 8 年前
        1
  •  1
  •   Rishav Singla    8 年前

    你必须使用 对话框.关闭() 在正确的地方。只有在“用户pin正确”时才会减少对话框,但当“pin尝试成功”时,您不会关闭对话框。

     @Override
    public void afterTextChanged(Editable s) {
    
        m_Text = input.getText().toString();
    
        if (m_Text.length() == details.get(2).length()) {
            if (!input.getText().toString().equals(details.get(2))) {
                Toast.makeText(MainActivity.mainActivity, "Incorrect pin entered!", Toast.LENGTH_SHORT).show();
                Log.d("eLOQ", "User pin is not correct");
    
                input.setText("");
    
            } else if (input.getText().toString().equals(details.get(2))) {
                controller.startStageTwo();
                Log.d("eLOQ", "User pin is correct");
         }
         dialog.dismiss();     // dismiss your dialog here. Here your dialog will dismiss in both caases
        }
    
    
    }
    

        2
  •  0
  •   Bholendra Singh    8 年前
    dialog.hide(); or dialog.cancel();