简体   繁体   English

长按启用 EditText(或按钮)

[英]Enable EditText(or button) on Long pressed

in my application EditText is there which is disable and I want to implement long press option on this edittext(while disable mode) which enable it and allowing to enter the character from softkey.在我的应用程序中 EditText 是禁用的,我想在这个编辑文本上实现长按选项(禁用模式),启用它并允许从软键输入字符。

Example:-例子:-

Suppose initially I am allowing user to enter some number into the EditText.假设最初我允许用户在 EditText 中输入一些数字。 After some operation, I need to disable this EditText.一些操作后,我需要禁用这个EditText。 Again if user want to change the number which previously he enter in the editText then first he need to long press on this editText.同样,如果用户想要更改他之前在 editText 中输入的数字,那么首先他需要长按此 editText。 After doing long press on this editText, editText get enable and again user will able to change or retype the number.长按此editText 后,editText 将启用,用户将能够再次更改或重新键入数字。 I need to do some operation before changing the number in the editText and during operation user not have any option to change the number in the editText.我需要在更改editText 中的数字之前进行一些操作,并且在操作期间用户没有任何选项可以更改editText 中的数字。

Code:-代码:-

<EditText
        android:id="@+id/eTextBillNoFrmReturn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_weight="7"
        android:clickable="true"
        android:background="@drawable/custom_edit_text"
        android:inputType="number" />

@Override
public boolean onLongClick(View v) {
    // do some operation
    return false;
}

but this code work only EditText is enable.但此代码仅在启用 EditText 时起作用。 Long press not work when EditText is disable.禁用 EditText 时长按不起作用。

Try this one.

@Override
public boolean onLongClick(View v) {
    // do some operation
btnname.setenable(true);
btnname.setfocusable(true);
    return false;
}

In case anyone stumbles across this old post like I did, I thought I would post an answer since I found some settings that worked well for my situation.如果有人像我一样偶然发现了这篇旧帖子,我想我会发布一个答案,因为我发现了一些适合我的情况的设置。

The OP was asking for a way to make the EditText function as if it is disabled yet still allow setting a long-click listener that can get called. OP 正在寻求一种方法来使 EditText 功能好像被禁用但仍然允许设置可以被调用的长按侦听器。

Set the edit text to be NOT focusable and set the input type to none.将编辑文本设置为不可聚焦并将输入类型设置为无。 The input type to none will make it so that a cursor doesn't try to show up when clicking on it, so it looks a bit nicer than the cursor that flashes for a second with just the focusable set to false.输入类型为 none 将使光标在单击时不会尝试显示,因此它看起来比仅将可聚焦设置为 false 的闪烁一秒钟的光标更好一些。

To do this in the XML:要在 XML 中执行此操作:

<EditText
    android:id="@+id/txtYourEditTextID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:inputType="none" />

To do this in the code:要在代码中执行此操作:

EditText txtYourEditText = (EditText)findViewById(R.id.txtYourEditTextID);
txtYourEditText.setFocusable(false);
txtYourEditText.setInputType(InputType.TYPE_NULL);

To do what OP is wanting, you will implement a long-click listener:要执行 OP 想要的操作,您将实现一个长按侦听器:

txtYourEditText.setOnLongClickListener(new View.OnLongClickListener {
    @Override
    public boolean onLongClick(View v) {
        ((EditText)v).setInputType(InputType.TYPE_CLASS_NUMBER);
        v.setFocusable(true);
        return true; //or return false if you do not want to consume the event
    }
});

Then on whatever the action is that you want to disable it again, call然后,无论您想再次禁用它的操作是什么,请致电

txtYourEditText.setFocusable(false);
txtYourEditText.setInputType(InputType.TYPE_NULL);

Hopefully this will help someone who is trying to do something similar.希望这会帮助那些试图做类似事情的人。

Have you tried to replace你有没有尝试更换

return false;

by经过

return true;

on your onLongClick(View v) method ?在您的onLongClick(View v)方法上?

使用与您的情况完美契合的切换按钮 切换按钮

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

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