Can anyone tell me how to make an EditText
not editable via XML? I tried setting android:editable
to false
, but
You could use android:editable="false"
but I would really advise you
to use setEnabled(false)
as it provides a visual clue to the user that
the control cannot be edited. The same visual cue is used by all
disabled widgets and consistency is good.
<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" />
and use following :
txtDate.setKeyListener(null);
I've tried the following:
codeEditText.setInputType(InputType.TYPE_NULL);
this.codeEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
pickCode();
}
}
});
this.codeEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pickCode();
}
});
but the problem was that if the edit text is the first in the form then it gets the focus and the pickCode() code which launches a new activity is called straight away. So I modified the code as follows and it seems to work quite well (except I cannot set the focus on the text edit but I don't need to):
itemCodeEditText.setFocusable(false);
this.itemCodeEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pickItem();
}
});
Best Regards,
Comments welcome,
John Goche
Let your Edittext be
EditText editText;
editText.setKeyListener(null);
will work but it just not listening to keys.
User can see a cursor on the edittext.
You can try editText.setEnabled(false);
That means user cannot see the cursor. To simply say it will became TextView
.
As mentioned in other answers, you can do a setEnabled(false)
but since you are asking how to set it via XML, here is how to do it.
Add the following attribute to your EditText
:
android:enabled="false"
I resolve this with a dialog.
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();
}
});