禁用EditText可编辑性和焦点(如TextView)

Chr*_*ris 11 xml android android-widget

有没有办法EditTextTextViewAndroid 一样制作行为(XML是首选)?我尝试过以下方法:

 android:editable="false"
 android:focusable="false"
 android:focusableInTouchMode="false"
 android:cursorVisible="false"
 android:longClickable="false"
Run Code Online (Sandbox Code Playgroud)

这有效,但我仍然可以触摸EditText到获得焦点(橙色寄宿生),虽然一旦我移开我的手指焦点丢失.我不确定是什么focusableInTouchMode,但是当我不停地触摸它时它不会消除焦点.

我不使用白色背景的原因TextView是它TextView的背景很难看.EditText背景有圆角和阴影效果.

提前致谢.

Mat*_*lis 10

EditText并且TextView非常相似.我看到硬编码到EditText.java中的唯一区别是将可编辑的默认值设置为true,您已手动设置.除此之外,EditText 风格是:

<style name="Widget.EditText">
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:clickable">true</item>
    <item name="android:background">@android:drawable/edit_text</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
    <item name="android:textColor">@android:color/primary_text_light</item>
    <item name="android:gravity">center_vertical</item>
</style>
Run Code Online (Sandbox Code Playgroud)

TextView是:

<style name="Widget.TextView">
    <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我的猜测是@android:drawable/edit_text橙色盒子的来源.实际上,它包含:

<item android:state_pressed="true" android:drawable="@drawable/textfield_pressed"/>
Run Code Online (Sandbox Code Playgroud)

最简单的方法可能是将其背景设置为默认值:

android:background="@android:drawable/textfield_default"

  • 大!所以答案是关闭android:clickable?顺便说一下,引用textfield_pressed的正确方法是@android:drawable/textfield_pressed (2认同)

小智 5

禁用EditText的可编辑性和焦点(例如TextView)。

尝试这个 :

android:longClickable="false"
android:clickable="false"    
android:cursorVisible="false"
android:editable="false"
Run Code Online (Sandbox Code Playgroud)