How to make EditText not editable through XML in Android?

后端 未结 27 1634
梦谈多话
梦谈多话 2020-11-28 17:35

Can anyone tell me how to make an EditText not editable via XML? I tried setting android:editable to false, but

  1. it is de
相关标签:
27条回答
  • 2020-11-28 18:12

    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.

    0 讨论(0)
  • 2020-11-28 18:13
    <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);
    
    0 讨论(0)
  • 2020-11-28 18:16

    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

    0 讨论(0)
  • 2020-11-28 18:17

    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.

    0 讨论(0)
  • 2020-11-28 18:19

    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"
    
    0 讨论(0)
  • 2020-11-28 18:19

    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();
    
                }
            });
    
    0 讨论(0)
提交回复
热议问题