第十五篇-EditText做简单的登录框
TextView和EditText的简单应用。
MainActivity.java
package com.example.aimee.edittexttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout);
}
}
layout.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phonenumber"
android:textSize="24sp"/>
<EditText
android:layout_width="196dp"
android:layout_height="wrap_content"
android:background="@drawable/edit_bg"//设置编辑框背景为矩形框
android:hint="@string/phone"
android:imeOptions="actionGo"
android:inputType="phone"
android:maxLength="40"
android:textColorHint="#FFFF0000"
android:textSize="24sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/passwordtext"
android:textSize="24sp"
android:layout_marginTop="30dp" />
<EditText
android:layout_width="196dp"
android:layout_height="wrap_content"
android:background="@drawable/edit_bg"
android:hint="@string/password"
android:imeOptions="actionSearch"
android:inputType="textPassword"
android:maxLength="40"
android:textColorHint="#FF000000"
android:textSize="24sp" />
</LinearLayout>
输入密码会用点来代替。
edit_bg.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#EFEFEF"/> <corners android:radius="3dip"/> <stroke android:width="0.5px" android:color="#505050"/> </shape> </item> </layer-list>
属性的含义:
android:imeOptions="flagNoExtractUi" //使软键盘不全屏显示,只占用一部分屏幕 同时,
这个属性还能控件软键盘右下角按键的显示内容,默认情况下为回车键
android:imeOptions="actionNone" //输入框右侧不带任何提示
android:imeOptions="actionGo" //右下角按键内容为'开始'
android:imeOptions="actionSearch" //右下角按键为放大镜图片,搜索
android:imeOptions="actionSend" //右下角按键内容为'发送'
android:imeOptions="actionNext" //右下角按键内容为'下一步' 或者下一项
android:imeOptions="actionDone" //右下角按键内容为'完成'
注意:如果设置了 键盘没有变化 那么需要单独加一些其他的属性 配合使用
xml中 属性设置:
1 将singleLine设置为true
2 将inputType设置为text
java代码设置
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
android:ems = "10" 的含义 :
指的是将对应的控件宽度设为10个字符的宽度。当设置该属性后,一行中最大只能显示设置的宽度。
如果想要实现用户名和编辑框在同一行,密码和编辑框也在同一行。
layout.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:gravity="center"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="23dp" android:text="用户名" /> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:background="@drawable/edit_bg" android:ems="10" android:textSize="23dp" android:hint="请输入用户名" android:inputType="textPersonName" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="23dp" android:text="密码" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:layout_marginVertical="20dp" android:background="@drawable/edit_bg" android:ems="10" android:textSize="23dp" android:hint="请输入密码" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="60dp" android:text="取消" /> </LinearLayout> </LinearLayout>
这是设置了3个LinearLayout为垂直排布,每个LinearLayout里面是水平排布。

浙公网安备 33010602011771号