【问题标题】:How to add a listener on edittext in a custom dialog如何在自定义对话框中的 edittext 上添加侦听器
【发布时间】:2016-12-06 12:03:42
【问题描述】:

我想实现一个对话框来接收密码输入并验证它。现在我已经定义了一个对话框类,oncreate 函数将使用我自己的布局文件设置内容视图。

    MyDialog class {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.ms_pdf_viewer_password_layout);
    }
}

这里是 XML

<EditText
    android:imeOptions="actionGo"
    android:layout_marginLeft="4dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:textColorHint="@color/hint_text_color"
    android:textColor="@color/text_color"
    android:textSize="12dp"
    android:hint="Please enter the password"
    android:ems="10"
    android:id="@+id/editText"/>

我在片段中创建了一个对话框对象,我想处理按下回车按钮并验证输入密码,但监听器不起作用,无法打印出 actionID。我想也许我没有正确获得 editText 对象。

 Dialog dialog = new PdfPasswordDialog(getActivity());
 View view = inflater.inflate(R.layout.ms_pdf_viewer_password_layout, container, false);
EditText editText = (EditText) dialog.findViewById(R.id.editText);



editText.setOnEditorActionListener(
            new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    Log.i(sClassTag,"actionID is "+actionId);

如何在自定义对话框的 edittext 上添加监听器来处理密码输入?任何帮助表示赞赏。

【问题讨论】:

  • 你可以使用TextWatcher

标签: android dialog android-edittext listener


【解决方案1】:

我终于用这条线解决了

 EditText editText = dialog.findViewById(R.id.edittext); 

之后

  dialog.show(). 

【讨论】:

    【解决方案2】:

    如果您想在输入新字符时更改或操作文本或检查输入的文本,您可以使用TextWatcher 这是一个示例:

    TextWatcher watcher = 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) {
                    // when the change is done
                }
            };
    

    【讨论】:

      【解决方案3】:

      使用此代码

      final Dialog dialog = new Dialog(getActivity());
      dialog.setContentView(R.layout.ms_pdf_viewer_password_layout);
      dialog.setTitle("Any Title You Want");
      EditText editText = (EditText) dialog.findViewById(R.id.editText);
      

      现在试试 onClickListener。

      【讨论】:

        猜你喜欢
        • 2017-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        相关资源
        最近更新 更多