谁能告诉我如何使一个EditText不可编辑通过XML?我试着设置android:editable为false,但是
已弃用;而且 但是没有成功。
谁能告诉我如何使一个EditText不可编辑通过XML?我试着设置android:editable为false,但是
已弃用;而且 但是没有成功。
当前回答
Kotlin扩展选项:
fun AppCompatAutoCompleteTextView.setEditable(editable: Boolean) {
if (!editable) {
this.tag = this.keyListener
this.keyListener = null
} else {
if(this.tag is KeyListener) {
this@setEditable.keyListener = this@setEditable.tag as KeyListener
}
}
}
其他回答
我用一个对话框来解决这个问题。
AlertDialog dialog;
EditText _editID;
TextView _txtID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
_txtID = (TextView) findViewById(R.id._txtID);
dialog = new AlertDialog.Builder(this).create();
_editID = new EditText(this);
_editID.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
dialog.setTitle("Numero do Registro:");
dialog.setView(_editID);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "IR", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
_txtID.setText(_editID.getText());
toId(Integer.parseInt((_editID.getText().toString())));
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCELAR", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
_txtID.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_txtID.setText("_editID.getText()");
//_editID.setText("");
dialog.show();
}
});
我使用EditText.setFocusable(false),如果我想编辑,再次设置为true。
android:editable已弃用,请改用inputType。
<EditText
android:id="@+id/editText_x"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="70"
android:inputType="none"
android:hint="@string/enter_x" />
如果你想点击它,但不想编辑它,试试:
android:focusable="false"
这个组合对我很有效:
<EditText ... >
android:longClickable="false"
android:cursorVisible="false"
android:focusableInTouchMode="false"
</EditText>