EditText 文本域改变事件监听

时间:2021-05-22 17:00:24

      mFileNameEt = (EditText) view.findViewById(R.id.fileNameEt);

   mFileNameEt.setText(fileName);

      mFileNameEt.setSelectAllOnFocus(true);


        mFileNameEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String fileName = mFileNameEt.getText().toString();
                if(hasFocus && !TextUtils.isEmpty(fileName)) {
                    int stop = fileName.lastIndexOf('.');
                    if(stop == -1) {
                        stop = fileName.length();
                    }
                    mFileNameEt.setSelection(0, stop); //选中文件名
                }
            }
        });