Make EditText ReadOnly

前端 未结 22 1297

I want to make a read-only EditText view. The XML to do this code seems to be android:editable=\"false\", but I want to do this in code.

H

22条回答
  •  感情败类
    2020-12-04 19:15

    This worked for me, taking several of the suggestions above into account. Makes the TextEdit focusable, but if user clicks or focuses, we show a list of selections in a PopupWindow. (We are replacing the wacky Spinner widget). TextEdit xml is very generic...

    public void onCreate(Bundle savedInstanceState) {
        ....  
        fEditState = (EditText) findViewById(R.id.state_edit);
    
        fEditState.setLongClickable(false);
        fEditState.setKeyListener(null);
        fEditState.setFocusable(true);
    
        fEditState.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus)
            {
                if (hasFocus)
                {
                   showStatesPopup();
    
                }
            }
        });
    
        fEditState.setOnClickListener(new Button.OnClickListener(){
    
            public void onClick(View arg0)
            {
               showStatesPopup();
            }
        });
        ....
    }
    
    private void showStatesPopup()
    {
        // fPopupWindowStates instantiated in OnCreate()
    
        if (!fPopupWindowStates.isShowing()) {
            // show the list view as dropdown
            fPopupWindowStates.showAsDropDown(fEditState, -5, 0);
        }
    }  
    

提交回复
热议问题