Android 用户单击我的 EditText 后如何执行某些操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2119072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:37:22  来源:igfitidea点击:

How to do something after user clicks on my EditText

androidandroid-edittextonclicklistener

提问by fhucho

I have an EditTextthat shows time. After user clicks the EditTextI want to show a TimePickerDialog, so I set a View.OnClickListenerto my EditText.

我有一个EditText显示时间的。在用户单击EditText我想显示 a 后TimePickerDialog,我将 a 设置View.OnClickListener为我的EditText.

But the OnClickListeneris behaving weirdly - I touch the EditTextand then software keyboard appears (which I don't want). When I touch again, OnClickListener.onClick()is finally called and the dialog appears.

但是OnClickListener它的行为很奇怪 - 我触摸EditText然后出现软件键盘(我不想要)。当我再次触摸时,OnClickListener.onClick()终于被调用并出现对话框。

What should I do if I want the dialog to appear immediately?

如果我想立即出现对话框,我该怎么办?

回答by Kai

Unlike most other controls, EditTexts are focusable while the system is in 'touch mode'. The first click event focuses the control, while the second click event actually fires the OnClickListener. If you disable touch-mode focus with the android:focusableInTouchModeView attribute, the OnClickListenershould fire as expected.

与大多数其他控件不同,EditText当系统处于“触摸模式”时,s 是可聚焦的。第一个点击事件聚焦控件,而第二个点击事件实际上触发OnClickListener. 如果您使用android:focusableInTouchModeView 属性禁用触摸模式焦点,则OnClickListener应按预期触发。

<EditText
        android:text="@+id/EditText01"
        android:id="@+id/EditText01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="false" />

回答by Warpzit

Another solution is to use the ontouchlistener:

另一种解决方案是使用ontouchlistener

edittext.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(MotionEvent.ACTION_UP == event.getAction()) {
            mQuaternion_1.setText("" + mQ1);
        }

        return true; // return is important...
    }
});

If it returns truethe event is handled and keyboard wont popup. If you'd want the keyboard to still popup and register click you'd have it return false.

如果它返回true,则处理事件并且键盘不会弹出。如果您希望键盘仍然弹出并注册点击,您将让它返回false

回答by Mark B

It sounds like you don't want the user to actually be able to type in the EditText. You just want them to be able to pick a time via a time picker. So why not just a button that pops up a TimePickerDialog? You could display the time that was picked in a TextView.

听起来您不希望用户实际上能够输入 EditText。您只希望他们能够通过时间选择器选择时间。那么为什么不只是一个弹出 TimePickerDialog 的按钮呢?您可以显示在 TextView 中选取的时间。

Or you could just replace the EditText view with a TimePickerview (not a dialog, just a regular view).

或者您可以只用TimePicker视图(不是对话框,只是常规视图)替换 EditText 视图。

回答by Kumar VL

Instead of touch Listener use Text Change Listener:

使用文本更改侦听器代替触摸侦听器:

editText.addTextChangedListener(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) {
            //yourFunction();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

回答by fhucho

I solved this by using a customized Button like this:

我通过使用这样的自定义按钮解决了这个问题:

<Button
        android:id="@+id/btTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="test"
        android:textSize="20dp"
        android:background="@android:drawable/edit_text" />

回答by Diego Torres Milano

If I understand correctly you just need something like

如果我理解正确,你只需要像

<EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:clickable="true" android:inputType="none" />

not editable and clickable. Set the OnClickListenerand you're done. In theory, in the practice you should add too

不可编辑和点击。设置OnClickListener,你就完成了。理论上,在实践中你也应该添加

android:editable="false" 

which is deprecated but does the trick.

这已被弃用,但确实有效。