Android学习:EditText用法

来源:互联网 发布:龙城霸业 boss积分数据 编辑:程序博客网 时间:2024/05/16 17:44

EditText是接受用户输入信息的最重要控件,首先在xml中定义了两个EditText和两个Button,其中通过Button的绑定事件来控制EditText是否能获得焦点。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    >    <EditText        android:id="@+id/edit1"        android:layout_width="match_parent"        android:layout_height="70dp"        android:maxLength="3"        android:textColor="#0000FF"        android:focusable="true"        android:hint="请输入您的帐号"        />    <EditText        android:id="@+id/edit2"        android:layout_width="match_parent"        android:layout_height="70dp"        android:textColor="#0000FF"        android:hint="请输入您的密码">        <!--下面这个属性可以控制哪个EditText优先获得焦点-->        <requestFocus />    </EditText>    <Button        android:id="@+id/button_1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAllCaps="false"        android:text="edit1失去焦点"        />    <Button        android:id="@+id/button_2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAllCaps="false"        android:text="edit1重新获取焦点"        /></LinearLayout>

在MainActivity中代码:

package com.example.whs.textandroid;import android.content.Context;import android.graphics.Color;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.view.Gravity;import android.view.KeyEvent;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_activity);        final EditText text = (EditText)findViewById(R.id.edit1);        //失去焦点        Button btn1 = (Button)findViewById(R.id.button_1);        btn1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                text.setFocusable(false);            }        });        //重新获得焦点        Button btn2 = (Button)findViewById(R.id.button_2);        btn2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                text.setFocusable(true);                text.setFocusableInTouchMode(true);                text.requestFocus();                text.findFocus();            }        });        //监听控件的焦点改变事件        text.setOnFocusChangeListener(new View.OnFocusChangeListener() {            @Override            public void onFocusChange(View view, boolean b) {                //获取触发事件的EditText                EditText edit = (EditText)view;                //如果失去焦点                if (b == false){                    InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);                    manager.hideSoftInputFromWindow(view.getWindowToken(),0);                    Toast.makeText(MainActivity.this, "edit1失去焦点", Toast.LENGTH_SHORT).show();                }else {                    Toast.makeText(MainActivity.this, "edit1获得焦点", Toast.LENGTH_SHORT).show();                }            }        });        //每当TextView的文本改变时,TextWatcher的下面几个方法就会被调用。        text.addTextChangedListener(new TextWatcher() {            @Override            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {                //这个方法被调用,说明在charSequence字符串中,从start位置开始的count个                //字符即将被长度为after的新文本所取代。在这个方法里面改变charSequence,会报错。            }            @Override            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {                //这个方法被调用,说明在charSequence字符串中,从start位置开始的count个                //字符刚刚取代了长度为before的旧文本。在这个方法里面改变charSequence,会报错。            }            @Override            public void afterTextChanged(Editable editable) {                //这个方法被调用,那么说明editable字符串的某个地方已经被改变。                int len = editable.toString().length();                if (len >= 3){                   Toast.makeText(MainActivity.this, "字符不能超过3个", Toast.LENGTH_SHORT).show();                }            }        });    }}
0 0
原创粉丝点击