How to make EditText not editable through XML in Android?

后端 未结 27 1844
梦谈多话
梦谈多话 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: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

提交回复
热议问题