問題描述
您好,我正在制作自定義撥號器,所以我創建了自己的輸入板.
Hi I'm making custom dialer so I create my own input pad.
問題是如何禁用 EditText 但仍允許剪切/復制/粘貼?股票撥號器可以做到這一點.
The problem is how do I disable the EditText but still allow cut/copy/paste? The stock dialer can do this.
我試過 android:focusable="false"
但它禁用剪切/復制(但仍然可以粘貼).
I have tried android:focusable="false"
but it disables cut/copy (can still paste though).
我還嘗試以編程方式禁用 inputType
,這會禁用所有三個命令:
I also tried to disable the inputType
programatically which disables all three commands:
myEditText.setInputType(InputType.TYPE_NULL); //Can't cut/copy/paste
從清單中禁用它也不起作用:
Disabling it from manifest also doesn't work:
android:configChanges="orientation|keyboardHidden" //Keyboard still popped up
有什么解決辦法嗎?謝謝
Any solution? Thanks
推薦答案
經過數小時的研究,我終于找到了適用于所有 API 版本的解決方案.希望這可以節省一些人的時間.
After hours and hours of research, I finally found a solution that works for all API versions. Hope this saves someone's time.
如果您正在為 API >= 11 進行開發,則解決方案很簡單:
If you are developing for API >= 11, the solution is simple, either:
1) 在EditText的xml文件中添加以下兩個屬性
1) Add the two properties below in the xml file of EditText
android:inputType="none"
android:textIsSelectable="true"
或
2) 以編程方式執行以下操作
2) Programatically do the below
myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setTextIsSelectable(true);
你已經完成了.
如果你想滿足 API <11 同樣,我發現如果您想選擇文本進行復制粘貼,則無法禁用鍵盤彈出.將 focusable 設置為 false 將禁用鍵盤,但它無濟于事,因為它也會禁用您選擇文本的能力.我在 stackoverflow 中找到的任何其他解決方案都不起作用或同時禁用文本選擇.
If you want to cater for API < 11 as well, I found that there is no way to disable to keyboard from popping out if you wanted to select the text for copy paste purpose. Setting focusable to false will disable the keyboard but it doesn't help because it disables your ability to select text too. Any other solutions I found in stackoverflow all either doesn't work or disables text selection at the same time too.
解決這個問題的一個丑陋的方法就是這樣......
One ugly way to solve this is as such..
首先,在EditText的xml文件中添加這個屬性
First, add this property in the xml file of EditText
android:editable="false"
是的,這已被棄用,但有必要在 API 版本中使 EditText 不可編輯
11.
Yes this is deprecated, but necessary for making the EditText not editable in API version < 11.
接下來,我們需要在鍵盤出現時立即隱藏它,這樣我們就可以繼續選擇文本而不會被鍵盤擋住.
Next, we will need to hide the keyboard as soon as it shows up, so that we can continue selecting text without the keyboard blocking the way.
使用下面的代碼檢測鍵盤是否出現(解決方案來自 https://stackoverflow.com/a/9108219/1241783),并立即隱藏它.
Use this code below to detect keyboard showing up (solution obtained from https://stackoverflow.com/a/9108219/1241783), and hide it immediately.
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
//Hide the keyboard instantly!
if (getCurrentFocus() != null)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
}
});
}
它適用于我的情況.盡管您可以在一瞬間看到鍵盤出現(這是丑陋的部分),但在撰寫本文時我想不出任何其他方法可以使其正常工作.如果您有更好的解決方案,請發表評論!
It works for my case. Though you can see the keyboard showing up in a split second (which is the ugly part) but I can't think of any other way to get this to work at the time of writing. If you have a better solution, please leave a comment!
如果這樣可以節省某人的時間,請告訴我:)
Let me know too if this saves someone's time :)
這篇關于從 EditText 禁用軟鍵盤但仍允許復制/粘貼?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!