前言
上一篇博客介绍了Android的TextView控件,这篇博客来说一下EditText控件。EditText为一个文本控件,提供了文本输入的功能,而且继承自TextView,可以理解为可以输入的TextView。因为继承的关系,很多TextView可以用到的方法,在EditText都可以用到。
EditText
对于EditText,在很多平台上都有用到,最大的用处就是供用户输入一些信息,所以主要的方法就两个:
- setText():设置TextView控件中显示的内容。
- getText() 获取TextView控件中显示的内容。
示例程序
现在通过两个示例程序,来讲解一下EditText的使用。
第一个例子,在EditText中插入表情图片,无论是开发任何系统,这个都是常用的实现。在编码之前,需要找到一些表情图片的资源,我这里就随机找了十张图片,注意资源文件的文件名必须是小写的,放在/res/drawable文件夹下。这样在清单文件R中,就可以看到与Drawable资源对于的资源清单ID,对于在清单文件中的资源,可以通过R类访问,但是访问到的为一个int类型的资源ID,如果需要访问详细内容,需要使用getResource()方法访问到所有的资源,在其中有特定资源的访问方法。关于资源清单文件R,以后再进行详细讲解。
在Android中,使用图片资源会用到一个Bitmap的类,此类代表一个位图资源,是一个final类,需要使用BitmapFactory类的静态方法decodeXxx()转化获得,此静态方法有多种重载模式,可以适应不同的资源来源。
下面直接上代码,对于布局而言,很简单的只有两个控件:
<?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" > <EditText
android:id="@+id/edImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/> <Button
android:id="@+id/btnInImg"
android:text="添加表情"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
实现InImageActivity.java代码:
package cn.bgxt.androiduiedittext; import java.util.Random; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class InImageActivity extends Activity { private Button btnInImg;
private EditText edImage;
//获取Drawable资源的Id数组
private final int[] DRAW_IMG_ID=
{
R.drawable.image0,
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8,
R.drawable.image9
};
public InImageActivity() {
// TODO Auto-generated constructor stub
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edittextinimg_activity); btnInImg=(Button)findViewById(R.id.btnInImg);
edImage=(EditText)findViewById(R.id.edImage); btnInImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 参数一个0-9的随机数
int random=new Random().nextInt(9);
//通过bitmapFactory获得位图资源
Bitmap bit=BitmapFactory.decodeResource(getResources(), DRAW_IMG_ID[random]);
//一个ImageSpan,用于插入的存放待插入的图片
ImageSpan imageSpan=new ImageSpan(InImageActivity.this,bit);
SpannableString spannableString=new SpannableString("img");
spannableString.setSpan(imageSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
edImage.append(spannableString);
}
}); } }
显示效果,点击按钮随机添加标签:
既然EditText主要是用来获取用户输入的信息的,那么第二个例子就来讲讲用户输入时候内容的验证吧。在XML Attribute中,有一些属性可以设置输入验证的范围内容,不过此为TextView类的属性,因为TextView无法输入,此处在EditText中讲解说明。
- android:digits:指定特定能被输入的字符。
- android:inputType:设定输入的类型,下面仅介绍一些常用的,多项可以使用“|”分割。
- textUri:必须是一个URL。
- textEmailAddress:Email地址
- textPassword:密码。
- number:数字。
- android:numeric:指定数字输入类型,多项可以使用“|”分割。
- integer:数字。
- decimal:浮点类型。
- signed:带符号。
以上属性仅仅是为了限制用户的输入,还有一些输入需要给用户以提示错误信息。这里将使用到setError()方法,如果设定了错误提示信息,会在EditText旁边以感叹号的形式显示。
布局代码:
<?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="使用Android:digits属性(仅输入数字与abcde)" /> <EditText
android:id="@+id/etNum"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:digits="123456789abcde"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用Android:inputtype属性(仅输入Email)" /> <EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:inputType="textPassword"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用Android:inputtype属性(仅输入密码)" /> <EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:numeric="decimal|signed"
/>
<Button
android:id="@+id/btnValidation"
android:text="验证第一个输入框是否为123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Java代码:
package cn.bgxt.androiduiedittext; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class EditValidationActivity extends Activity { private Button btnValidation;
private EditText etNum;
public EditValidationActivity() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.editvalidation_activity);
btnValidation=(Button)findViewById(R.id.btnValidation);
etNum=(EditText)findViewById(R.id.etNum); btnValidation.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String num=etNum.getText().toString().trim();
if(!num.equals("123"))
{
etNum.setError("请输入123");
}
}
}); }
}
效果展示:
如果点击验证按钮,而第一个文本框输入的不是123,则提示错误信息:
总结
以上就讲解了EditText在实际项目中常用的效果,虽然大部分使用的是TextView的属性设置的效果,但是Android下还有一些其他的供用户输入的控件,可以使用,所以才以这样的继承结构实现属性。
请支持原创,尊重原创,转载请注明出处。谢谢。