Fra*_*ick 291 android android-edittext
谁能告诉我如何EditText通过XML 进行不可编辑?我试着设置android:editable到false,但
Kri*_*adi 393
使用这个简单的代码:
textView.setKeyListener(null);
Run Code Online (Sandbox Code Playgroud)
有用.
编辑:要KeyListener稍后添加,请执行以下操作
1:将键监听器设置为标签 textView
textView.setTag(textView.getKeyListener());
textView.setKeyListener(null);
Run Code Online (Sandbox Code Playgroud)
2:从标签获取密钥监听器并将其设置回 textView
textView.setKeyListener((KeyListener) textView.getTag());
Run Code Online (Sandbox Code Playgroud)
Gal*_*e33 389
将其添加到EditText xml文件中:
<EditText ...
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false">
</EditText>
Run Code Online (Sandbox Code Playgroud)
它会起到同样的效果android:editable="false".为我工作,希望它也适合你.
Mah*_*ran 54
让你的Edittext成为
EditText editText;
Run Code Online (Sandbox Code Playgroud)
editText.setKeyListener(null);
会工作,但它只是没有听到钥匙.
用户可以在edittext上看到光标.
你可以试试 editText.setEnabled(false);
这意味着用户无法看到光标.简单地说它会成为TextView.
vid*_*ida 30
正如其他答案中所提到的,你可以做一个setEnabled(false) 但是因为你问的是如何通过XML设置它,这里是如何做到的.
将以下属性添加到您的EditText:
android:enabled="false"
Run Code Online (Sandbox Code Playgroud)
kou*_*nho 24
禁用XML(一行):
android:focusable="false"
Run Code Online (Sandbox Code Playgroud)
如果需要,也可以从Java重新启用(也是一行):
editText.setFocusableInTouchMode(true);
Run Code Online (Sandbox Code Playgroud)
Tim*_*çin 15
他们将"可编辑"弃用,但没有在inputType中提供相应的工作.
顺便说一句,inputType ="none"有效吗?就我看来,使用与否使用并没有任何区别.
例如,默认的editText被称为单行.但是你必须选择一个inputType作为单行.如果选择"无",它仍然是多行的.
Ado*_*ncz 13
正如你提到的android:editable已被弃用.应该使用android:inputType ="none"但由于错误而无效(https://code.google.com/p/android/issues/detail?id=2854)
但是你可以通过使用可聚焦来实现同样的目的.
通过XML:
<EditText ...
android:focusable="false"
</EditText>
Run Code Online (Sandbox Code Playgroud)
来自代码:
((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);
Run Code Online (Sandbox Code Playgroud)
Mas*_*ari 13
这个组合对我有用:
<EditText ... >
android:longClickable="false"
android:cursorVisible="false"
android:focusableInTouchMode="false"
</EditText>
Run Code Online (Sandbox Code Playgroud)
<EditText
android:id="@+id/txtDate"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:clickable="false"
android:cursorVisible="false"
android:gravity="center" />
Run Code Online (Sandbox Code Playgroud)
并使用以下:
txtDate.setKeyListener(null);
Run Code Online (Sandbox Code Playgroud)
小智 6
如果你想在java代码中执行它,只需使用此行禁用它:
editText.setEnabled(false);
Run Code Online (Sandbox Code Playgroud)
这样就可以了:
editText.setEnabled(true);
Run Code Online (Sandbox Code Playgroud)
我试着这样做:
textView.setInputType( InputType.TYPE_NULL );
Run Code Online (Sandbox Code Playgroud)
这应该工作,但对我来说它没有.
我完成了这段代码:
textView.setKeyListener(new NumberKeyListener() {
public int getInputType() {
return InputType.TYPE_NULL;
}
protected char[] getAcceptedChars() {
return new char[] {};
}
});
Run Code Online (Sandbox Code Playgroud)
哪作得很好.
| 归档时间: |
|
| 查看次数: |
274296 次 |
| 最近记录: |