繁体   English   中英

如何在EditText获得焦点后立即显示AlertDialog?

[英]How do I make my AlertDialog appear as soon as EditText has focus?

我希望我的EditText获得焦点后立即弹出AlertDialog 现在,我必须单击两次EditText

第一次单击EditText ,将出现软键盘(用于直接在Edittext键入)。 然后,第二次单击EditText ,出现AlertDialog

我怎么能拥有它,所以只需要在EditText上单击一下就可以弹出AlertDialog

这是我的代码:

在我的onCreate ...

//when user clicks on "commentName" EditText we want a new AlertDialog to open
commentName.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {

    AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
    builder.setTitle("Ur Comment:");

    //start the following xml file/ layout
    View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
    builder.setView(viewInflated);

    // Set up the buttons
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
      }
    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
      }
    });

    builder.show();

  }
});

我根据您的描述使用OnFocusChangeListener使它工作。

只需用以下内容替换问题中的整个代码段即可。 并在注释下方粘贴AlertDialog代码。

// commentName is your EditText
commentName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            return; // Prevents alert from being shown when losing focus.
        }
        // Your AlertDialog code goes here
    }
});

if (!hasFocus)用户单击其他使EditText失去焦点的东西, if (!hasFocus)阻止显示警报。

这为我做到了, OnTouchListener

 //when user clicks on "commentname" edittext we want a new textbox to open
    commentname.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {

        if(event.getAction() == MotionEvent.ACTION_UP) {
          AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
          builder.setTitle("Ur Comment:");

          //start the following xml file/ layout
          View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
          builder.setView(viewInflated);

          // Set up the buttons
          builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();
            }
          });
          builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              dialog.cancel();
            }
          });

          builder.show();
          return true;

        }
        return false;

      }
    });

使用最少的代码即可完成此操作的另一种方法是,只需禁用对EditText关注。 您可以通过将以下属性添加到EditText ...

<EditText
...
android:clickable="true"
android:focusable="false"/>

然后在您的EditText上设置OnClickListener ,并在onClick()方法中显示该对话框。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM