EditText onClickListener in Android

后端 未结 14 2062
时光取名叫无心
时光取名叫无心 2020-12-02 12:03

I want an EditText which creates a DatePicker when is pressed. So I write the following code:

    mEditInit = (EditText) findViewById(R.id.date_init);
    mE         


        
相关标签:
14条回答
  • 2020-12-02 12:23

    The following works perfectly for me.

    First set your date picker widget's input to 'none' to prevent the soft keyboard from popping up:

    <EditText android:inputType="none" ... ></EditText>
    

    Then add these event listeners to show the dialog containing the date picker:

    // Date picker
    EditText dateEdit = (EditText) findViewById(R.id.date);
    dateOfBirthEdit.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                showDialog(DIALOG_DATE_PICKER);
            }
            return false;
        }
    });
    
    dateEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
    
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                showDialog(DIALOG_DATE_PICKER);
            } else {
                dismissDialog(DIALOG_DATE_PICKER);
            }
        }
    });
    

    One last thing. To make sure typed days, months, or years are correctly copied from the date picker, call datePicker.clearFocus() before retrieving the values, for instance via getMonth().

    0 讨论(0)
  • 2020-12-02 12:27

    Here is the solution I implemented

    mPickDate.setOnKeyListener(new View.OnKeyListener() {
    
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            showDialog(DATE_DIALOG_ID);
            return false;
        }
    });
    

    OR

    mPickDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            showDialog(DATE_DIALOG_ID);
    
        }
    });
    

    See the differences by yourself. Problem is since (like RickNotFred said) TextView to display the date & edit via the DatePicker. TextEdit is not used for its primary purpose. If you want the DatePicker to re-pop up, you need to input delete (1st case) or de focus (2nd case).

    Ray

    0 讨论(0)
  • 2020-12-02 12:28

    Why did not anyone mention setOnTouchListener? Using setOnTouchListener is easy and all right, and just return true if the listener has consumed the event, false otherwise.

    0 讨论(0)
  • 2020-12-02 12:31

    This Works For me:

    mEditInit = (EditText) findViewById(R.id.date_init);
    mEditInit.setKeyListener(null);
    mEditInit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus)
                {
                    mEditInit.callOnClick();
                }
            }
        });
    mEditInit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DATEINIT_DIALOG);
        }
    
    });
    
    0 讨论(0)
  • 2020-12-02 12:32

    As Dillon Kearns suggested, setting focusable to false works fine. But if your goal is to cancel the keyboard when EditText is clicked, you might want to use:

    mEditText.setInputType(0);
    
    0 讨论(0)
  • 2020-12-02 12:37

    Normally, you want maximum compatibility with EditText's normal behaviour.

    So you should not use android:focusable="false" as, yes, the view will just not be focusable anymore which looks bad. The background drawable will not show its "pressed" state anymore, for example.

    What you should do instead is the following:

    myEditText.setInputType(InputType.TYPE_NULL);
    myEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // showMyDialog();
        }
    });
    myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                // showMyDialog();
            }
        }
    });
    

    By setting the input type to TYPE_NULL, you prevent the software keyboard from popping up.

    By setting the OnClickListener and OnFocusChangeListener, you make sure that your dialog will always open when the user clicks into the EditText field, both when it gains focus (first click) and on subsequent clicks.

    Just setting android:inputType="none" or setInputType(InputType.TYPE_NULL) is not always enough. For some devices, you should set android:editable="false" in XML as well, although it is deprecated. If it does not work anymore, it will just be ignored (as all XML attributes that are not supported).

    0 讨论(0)
提交回复
热议问题