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

时间:2018-12-12 00:56:26

标签: java android android-edittext

我希望AlertDialog聚焦后立即弹出我的EditText。现在,我必须单击两次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();

  }
});

3 个答案:

答案 0 :(得分:2)

我让它根据您的描述使用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失去焦点的警报时显示警报。

答案 1 :(得分:0)

这对我来说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;

      }
    });

答案 2 :(得分:0)

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

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

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