Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在EditText具有焦点时立即显示AlertDialog?_Java_Android_Android Edittext - Fatal编程技术网

Java 如何在EditText具有焦点时立即显示AlertDialog?

Java 如何在EditText具有焦点时立即显示AlertDialog?,java,android,android-edittext,Java,Android,Android Edittext,我希望我的编辑文本有焦点时,我的警报对话框立即弹出。现在,我必须点击我的编辑文本两次 第一次单击我的EditText,软键盘就会出现(用于直接在我的EditText中键入)。然后,第二次单击编辑文本时,会出现报警对话框 我怎么能只需点击一下编辑文本,弹出警报对话框 这是我的密码: 在我的onCreate中 //when user clicks on "commentName" EditText we want a new AlertDialog to open commentName.setOn

我希望我的
编辑文本
有焦点时,我的
警报对话框
立即弹出。现在,我必须点击我的编辑文本两次

第一次单击我的
EditText
,软键盘就会出现(用于直接在我的
EditText
中键入)。然后,第二次单击编辑文本时,会出现报警对话框

我怎么能只需点击一下
编辑文本
,弹出
警报对话框

这是我的密码:

在我的
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();

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

根据你对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)
防止在用户单击其他使编辑文本失去焦点的内容时显示警报。

根据您对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)
防止在用户单击其他使编辑文本失去焦点的内容时显示警报。

这为我做到了,
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;

      }
    });

这是为我而做的,
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

//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();

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


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

另一种方法是只需禁用
EditText
上的焦点,就可以用最少的代码完成此操作。您可以通过将下面的属性添加到您的
EditText

//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();

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


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

您是否尝试过
onFocusChange()
onTouch()
事件?您是否尝试过
onFocusChange()
onTouch()
事件?嘿,很棒的提示,谢谢,如果不是你让我用不同的方式思考,它就不会工作得完美。不过,上面的一个问题是——如果用户在弹出窗口上单击“取消”,edittext仍然有焦点,然后改变主意再次写入并单击edittext,弹出窗口不会出现……我用setOnTouchListener.Tx解决了这个问题。嘿,很好的提示,谢谢,如果你没有让我用不同的思维方式,它就不会工作得很好。但上面的一个问题是,如果用户单击弹出窗口上的“取消”,edittext仍然有焦点,然后改变主意再次写入,并单击edittext,弹出窗口不会出现…我用setOnTouchListener.Tx解决了这个问题。