EditText控件的使用

android:hint="这里提示你该输入什么内容"
android:textColorHint="设置提示内容颜色"
android:maxLength="输入最长的长度"
android:minLength="输入最短的长度"
andrioid:inputType="输入数据类型,便于输入法显示适用的键盘"
    android:inputType="none"//输啥都行
    android:inputType="text"//输入文本
    android:inputType="textPassword"//输入密码
    android:inputType="number"//输入数字
    android:inputType="phone"//拨号键盘
    android:inputType="date"//日期键盘 etc....

示例:

<?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">  //线性布局中控制垂直或者水平布局

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test,test,test"/>

    <!-- 用于输入数字的文本框 -->
    <EditText
        android:id="@+id/editText1"
        android:inputType="number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLength="11"
        android:hint="请在此输入手机号码"
        android:textColorHint="#55000000"/>

    <!-- 用于输入密码的文本框 -->
    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLength="8"
        android:hint="输入密码"
        android:textColorHint="#55FF0000"
        android:inputType="textPassword"/>

</LinearLayout>

示例效果:

同时activity也对EditText有一些操作,代码如下:

package cn.edu.qtech.csc.lcb.edittextdemo;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView textview;
    private EditText edittext_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layouttest);

        textview = (TextView)findViewById(R.id.textView1);
        edittext_num = (EditText)findViewById(R.id.editText1);

        //edittext_num.setText("1234567");

        edittext_num.addTextChangedListener(new TextWatcher() {
            //在字符串s中,从start开始的长度为count的子串正在替换长度为before的子串
            //不要在此改变EditText的值!!!!!!!!!
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                textview.setText("文本框输入:"+edittext_num.getText().toString());
            }
            //在字符串s中,从start开始的长度为count的子串将要被长度为after的子串替代
            //不要在此改变EditText的值!!!!!!!!!!!
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub
            }
            //字符串内容变动后触发,此处可以更改EditText的内容----但是注意不要死循环
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                String ts=s.toString();
                if (ts.length()<3) //长度小于3,则不处理
                    return;
                if (ts.substring(ts.length()-3).compareTo("123")==0)  //发现末尾"123"则删除
                {
                    edittext_num.setText(ts.substring(0, ts.length()-3)); //删除"123"
                    edittext_num.setSelection(ts.length()-3); //设定光标位置----否则光标回到行首
                }
            }
        });
    }

}

控件的基本使用模板:https://download.csdn.net/download/qq_38367681/10768677

猜你喜欢

转载自blog.csdn.net/qq_38367681/article/details/83097796