-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:maxLength=“40”
-
android:hint=“请输入用户名…”
-
android:textColorHint=“#238745”/>
-
</LinearLayout>
运行程序效果如下:
看到了吧,颜色已经变了。。
- 还有一个比较实用的功能,就是设置EditText的不可编辑。设置android:enabled="false"可以实现不可编辑,可以获得焦点。这时候我们看到EditText和一个TextView差不多:
- 实现类似html中Textarea的文本域。在Android中没有专门的文本域组件,但是可以通过设置EditText的高来实现同样的文本域功能。修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“200dip”/>
-
</LinearLayout>
运行程序效果如下:
四:输入特殊格式的字符
在我们开发程序的时候不免会输入一些特属个数的字符,比如密码(输入框的字符要加密显示),电话号码(比如数字和-),数字等,这些都算是一些特属格式的字符,强大的EditText同样为我们提供了输入这些特属格式字符的设置。
- 密码文本框。密码输入也是Android应用常用的功能,通过配置EditText的android:password="true"就可以实现这一密码输入功能,修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:password=“true”/>
-
</LinearLayout>
运行效果如下:
可以看到我们输入的字符已经被“.”这样的掩码所代替。
- 手机中发短信打电话是必不可少的,所以用于专门输入电话号码的文本框也是大有用途,有了他我们对是否是电话号码的校验就容易的多了(因为字符是正确的,只要校验格式 ).通过设置android:phoneNumber="true"就可以把EditText变成只接受电话号码输入的文本框,连软键盘都已经变成拨号专用软键盘了,所以不用再担心输入其他字符了。修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:phoneNumber=“true”/>
-
</LinearLayout>
运行程序效果如下:
注意看软键盘,已经变成拨号专用的啦.
- 有时候我们只想输入数字,不想输入字母,EditText为我们提供了android:numeric来控制输入的数字类型,一共有三种分别为integer(正整数)、signed(带符号整数)和decimal(浮点数)。这里以signed类型的为例,修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:numeric=“signed”/>
-
</LinearLayout>
运行效果如下:
注意这里的软键盘变成“数字键盘”的变化.
五:为文本指定特定的软键盘类型
前面我们通过指定为电话号码特定格式,然后键盘类型变成了拨号专用的键盘,这个是自动变的,其实我们也可以通过android:inputType来设置文本的类型,让输入法选择合适的软键盘的。。android:inputType有很多类型,这里使用date类型来演示,修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:inputType=“date”/>
-
</LinearLayout>
运行效果如下:
六:Enter键图标的设置
软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:
-
actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:
-
actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:
-
actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:
-
actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果:
-
actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:
-
actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:
-
actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:
下面已搜索为例,演示一个实例,修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:imeOptions=“actionSearch”/>
-
</LinearLayout>
修改HelloEditText如下:
-
package com.flysnow;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.KeyEvent;
-
import android.widget.EditText;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
import android.widget.TextView.OnEditorActionListener;
-
public class HelloEditText extends Activity {
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
EditText editText=(EditText)findViewById(R.id.edit_text);
-
editText.setOnEditorActionListener(new OnEditorActionListener() {
-
@Override
-
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
-
Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();
-
return false;
-
}
-
});
-
}
-
}
运行程序,点击回车(也就是搜索图标软键盘按钮)会显示该actionId.我们上面的每一个设置都会对应一个常量,这里的actionId就是那个常量值。
七:EditText的取值、全选、部分选择、获取选中文本
下面通过一个例子来演示EditText的取值、全选、部分选择和获取选中文本.main.xml修改如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:imeOptions=“actionSearch”/>
-
<Button
-
android:id=“@+id/btn_get_value”
-
android:text=“取值”
-
android:layout_width=“wrap_content”
-
android:layout_height=“wrap_content”/>
-
<Button
-
android:id=“@+id/btn_all”
-
android:text=“全选”
-
android:layout_width=“wrap_content”
-
android:layout_height=“wrap_content”/>
-
<Button
-
android:id=“@+id/btn_select”
-
android:text=“从第2个字符开始选择”
-
android:layout_width=“wrap_content”
-
android:layout_height=“wrap_content”/>
-
<Button
-
android:id=“@+id/btn_get_select”
-
android:text=“获取选中文本”
-
android:layout_width=“wrap_content”
-
android:layout_height=“wrap_content”/>
-
</LinearLayout>
HelloEditText修改如下:
-
package com.flysnow;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.text.Editable;
-
import android.text.Selection;
-
import android.view.KeyEvent;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.EditText;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
import android.widget.TextView.OnEditorActionListener;
-
/**
-
* EditText演示
-
* @author 飞雪无情
-
* @since 2010-11-29
-
*/
-
public class HelloEditText extends Activity {
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
final EditText editText=(EditText)findViewById(R.id.edit_text);
-
//监听回车键
-
editText.setOnEditorActionListener(new OnEditorActionListener() {
-
@Override
-
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
-
Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();
-
return false;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
设计模式学习笔记
设计模式系列学习视频
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
344027233)]
[外链图片转存中…(img-KefHpgAK-1712344027233)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
设计模式学习笔记
[外链图片转存中…(img-nkhWBU45-1712344027233)]
设计模式系列学习视频
[外链图片转存中…(img-Rdz6zE3t-1712344027233)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!