EditText控件及相关操作

<pre name="code" class="html"><EditText
        android:id="@+id/edit_text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button3"
        android:drawableLeft="@mipmap/ic_launcher"
        android:hint="请输入账号"
        android:inputType="number"
        android:lines="2"
        android:text="123456dd" />

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)