EditText控件及相关操作

hint是默认灰色的提示信息 
drawableLeft可在文本框内插入图片
inputType是能在文本框内输入字符的类型
lines是文本框显示几列

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.edit_text1);

//        文本框改变时触发事件
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

//        按回车触发事件
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                Toast.makeText(MainActivity.this, v.getText(), Toast.LENGTH_SHORT).show();

                return true;
            }
        });

    }


 
 

你可能感兴趣的:(Android,基础UI组件)