Android development-Android common components-EditText input box

4.2 EditText (input box)

  • EditText input box, inherited from TextView, also inherits its properties
  • EditText specific properties:

genus name

illustrate

android:hint

default prompt text

android:textColorHint

The color of the default hint text

android:selectAllOnFocus

Boolean value. After clicking the input box to get the focus, get all the text content in the input box

android:inputType

Limit the data entered

android:minLines

Set the minimum number of rows

android:maxLines

Set the maximum number of lines PS: When the input content exceeds maxline, the text will automatically scroll up! !

android:singleLine

Only allows single-line input without scrolling

android:textScaleX

Set the horizontal interval between words

android:textScaleY

Set the vertical interval between words

android:capitalize

(deprecated)

sentences : only the first letter is capitalized; words : the first letter of each word is capitalized, and spaces are used to distinguish words; characters: every English letter is capitalized

    •   Text type, mostly uppercase, lowercase and numeric symbols

android:inputType="none"

none

android:inputType="text"

text

android:inputType="textCapCharacters"

character uppercase

android:inputType="textCapWords"

capital letters

android:inputType="textCapSentences"

sentence capitalization

android:inputType="textAutoCorrect"

Text autocorrect

android:inputType="textAutoComplete"

text autocomplete

android:inputType="textMultiLine"

multiline text

android:inputType="textImeMultiLine"

multiline text

android:inputType="textNoSuggestions"

text no suggestion

android:inputType="textUri"

Link

android:inputType="textEmailAddress"

email address

android:inputType="textEmailSubject"

email subject

android:inputType="textShortMessage"

Text Message

android:inputType="textLongMessage"

long message

android:inputType="textPersonName"

person's name

android:inputType="textPostalAddress"

mailing address

android:inputType="textPassword"

password

android:inputType="textVisiblePassword"

visible password

android:inputType="textWebEditText"

Web edit text

android:inputType="textFilter"

text filtering

android:inputType="textPhonetic"

Text Pinyin

    •   value type
android:inputType="number"//数字
android:inputType="numberSigned"//数字符号
android:inputType="numberDecimal"//十进制数字
android:inputType="phone"//拨号键盘
android:inputType="datetime"//日期时间
android:inputType="date"//日期键盘
android:inputType="time"//时间键盘
  • 设置EditText获得焦点,同时弹出小键盘
edit.requestFocus(); //请求获取焦点
edit.clearFocus(); //清除焦点
  • 低版本的系统直接requestFocus就会自动弹出小键盘了稍微高一点的版本则需要我们手动地去弹键盘。
    •   第一种:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    •   第二种:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
  • EditText光标位置的控制
setSelection();//一个参数的是设置光标位置的,两个参数的是设置起始位置与结束位置的中  间括的部分,即部分选中

示例:

edit_text.xml:

<?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"
    android:background="#eeeeee"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="40sp"
        android:text="登录界面"
        android:textColor="#000000"
        android:textSize="20sp"/>
    <!--    用户名-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="用户名:"
            android:textColor="#000000"
            android:textSize="19sp"/>
        <EditText
            android:id="@+id/etName"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="4"
            android:text="临易ioup"
            android:hint="请输入用户名"/>
    </LinearLayout>
    <!--    密码-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="密    码:"
            android:textColor="#000000"
            android:textSize="19sp"/>
        <EditText
            android:id="@+id/etPassword"
            android:layout_width="0dp"
            android:inputType="textPassword"
            android:layout_height="50dp"
            android:layout_weight="4"
            android:hint="请输入密码"/>
    </LinearLayout>
    
</LinearLayout>

MainActivity.java:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_text);
        EditText editName = findViewById(R.id.etName);
        editName.requestFocus();//设置一个焦点
        editName.setSelection(2);//将焦点定为第二个字后边

    }
}

启动测试:

Guess you like

Origin blog.csdn.net/LYly_B/article/details/129807814