| 本帖最后由 liu 于 2018-12-2 16:33 编辑 
 EditText
 
 
  
 
  
 
 
  
 
  
 EditText(输入框)是平时开发中经常使用到的一个控件,例如登录、注册页面等,它的属性有很多,这里只说一下一些经常使用的:
 
 
 android:height                           设置高度
 android:width                             设置宽度
 android:maxHeight                    设置文本区域的最大高度
 android:minHeight                     设置文本区域的最小高度
 android:maxWidth                     设置文本区域的最大宽度
 android:minWidth                      设置文本区域的最小宽度
 android:hint                                设置显示在控件上的提示信息
 android:textColor                       设置字体颜色
 android:textStyle                        设置字体风格
 android:textSize                         设置字体大小
 android:textColorHint                 设置提示信息文字的颜色
 android:textScaleX                     设置字之间的间距
 android:background                    设置背景
 android:editable                          设置是否可编辑
 android:inputType                       设置文本的类型
 android:lineSpacingExtra            设置行间距
 android:lineSpacingMultiplier      设置行间距的倍数
 android:cursorVisible                   设定光标为显示/隐藏,默认显示
 android:digits                               设置允许输入哪些字符 获取EditText中的内容
 
 xml:
 
 [XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">
    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@null"
        android:text="Hello World" />
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:background="@color/colorAccent"
        android:text="点击获取输入框内容"
        android:textColor="@android:color/white" />
</LinearLayout>
java代码:
 
 [Java] 纯文本查看 复制代码 package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    private EditText mEditText;
    private Button mButton;
    private String content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化控件
        mEditText = (EditText) findViewById(R.id.edittext);
        mButton = (Button) findViewById(R.id.button);
        //获取输入框中的内容
        content = mEditText.getText().toString();
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断输入框内容是否为空
                if (!TextUtils.isEmpty(content)) {
                    Toast.makeText(MainActivity.this, content, Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
对输入框内容变化的监听
 
 
 [Java] 纯文本查看 复制代码 package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    private EditText mEditText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化控件
        mEditText = (EditText) findViewById(R.id.edittext);
        mEditText.addTextChangedListener(new TextWatcher() {
            //内容发生改变之前的回调
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("TextChangedListener", "beforeTextChanged----------" + s.toString());
            }
            //内容发生改变时的回调
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("TextChangedListener", "onTextChanged----------" + s.toString());
            }
            //内容改变以后,用户没有继续输入时的回调
            @Override
            public void afterTextChanged(Editable s) {
                Log.i("TextChangedListener", "afterTextChanged----------" + s.toString());
            }
        });
    }
}
 
 
 
 |