[英]How disable an EditText and keep the background color to be white?
當我使用setEnabled(false)
或通過xml文件android:enabled="false"
禁用EditText時,背景顏色將為灰色:
如果我將背景更改為android:background="@color/white"
,則會丟失邊框:
所以我的問題是,如何禁用EditText(我不希望在長按它時彈出任何對話框)並保持邊框不變?
最好的選擇是創建自己的狀態列表Drawable,並將禁用的背景指定為與啟用的狀態相同。 您將需要創建自己的9補丁程序映像,或者還可以從Android開源中提取資源文件。
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
這是我用於EditText背景的示例:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:state_focused="true"
android:drawable="@drawable/edittext_bg_selected" />
<!-- Inactive tab -->
<item android:state_focused="false"
android:drawable="@drawable/edittext_bg_unselected" />
<item android:state_enabled="false"
android:drawable="@drawable/edittext_bg_unselected" />
<!-- Add all of your other states -->
</selector>
您只需要弄混不同的狀態即可創建適合您的功能。
我找到了一種方法:
android:editable="false"
android:cursorVisible="false"
editText.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
嘗試使用TextView
代替android:background="#FFFFFF"
。
您也可以嘗試android:focusable="false"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.