本文实例讲述了android编程开发之edittext中不输入特定字符会显示相关提示信息的方法。分享给大家供大家参考,具体如下:
先看效果图:
源码如下:
布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= ".mainactivity" >
<textview
android:id= "@+id/textview1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignparentleft= "true"
android:layout_alignparenttop= "true"
android:text= "@string/text_num" />
<textview
android:id= "@+id/textview2"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignparentleft= "true"
android:layout_below= "@+id/num"
android:text= "@string/text_abc" />
<edittext
android:id= "@+id/num"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_below= "@+id/textview1"
android:digits= "0123456789"
android:ems= "10" />
<edittext
android:id= "@+id/abc"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignleft= "@+id/textview2"
android:layout_below= "@+id/textview2"
android:digits= "qwertyuiopasdfghjklzxcvbnm"
android:ems= "10" >
</edittext>
<textview
android:id= "@+id/textview3"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignleft= "@+id/abc"
android:layout_below= "@+id/abc"
android:layout_margintop= "14dp"
android:text= "@string/text_num2" />
<edittext
android:id= "@+id/num2"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignleft= "@+id/textview3"
android:layout_below= "@+id/textview3"
android:ems= "10"
android:inputtype= "number|textcapcharacters" >
</edittext>
<button
android:id= "@+id/button1"
style= "?android:attr/buttonstylesmall"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_above= "@+id/textview2"
android:layout_below= "@+id/textview1"
android:layout_torightof= "@+id/num"
android:text= "确认1" />
<button
android:id= "@+id/button4"
style= "?android:attr/buttonstylesmall"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignbottom= "@+id/email"
android:layout_aligntop= "@+id/email"
android:layout_torightof= "@+id/email"
android:text= "确认4" />
<button
android:id= "@+id/button2"
style= "?android:attr/buttonstylesmall"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_above= "@+id/textview3"
android:layout_alignleft= "@+id/button1"
android:layout_below= "@+id/textview2"
android:text= "确认2" />
<button
android:id= "@+id/button3"
style= "?android:attr/buttonstylesmall"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignbottom= "@+id/num2"
android:layout_alignleft= "@+id/button2"
android:layout_aligntop= "@+id/num2"
android:text= "确认3" />
<textview
android:id= "@+id/textview4"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignparentleft= "true"
android:layout_centervertical= "true"
android:text= "@string/text_email" />
<edittext
android:id= "@+id/email"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignleft= "@+id/textview4"
android:layout_below= "@+id/textview4"
android:layout_margintop= "21dp"
android:ems= "10"
android:inputtype= "textemailaddress" >
</edittext>
</relativelayout>
|
mainactivity.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package com.example.edittext2;
import android.app.activity;
import android.os.bundle;
import android.view.menu;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
public class mainactivity extends activity {
private edittext num;
private edittext abc;
private edittext num2;
private edittext email;
private button button1;
private button button2;
private button button3;
private button button4;
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
num=(edittext) findviewbyid(r.id.num);
abc=(edittext) findviewbyid(r.id.abc);
num2=(edittext) findviewbyid(r.id.num2);
email=(edittext) findviewbyid(r.id.email);
button1=(button) findviewbyid(r.id.button1);
button2=(button) findviewbyid(r.id.button2);
button3=(button) findviewbyid(r.id.button3);
button4=(button) findviewbyid(r.id.button4);
button1.setonclicklistener( new view.onclicklistener() {
@override
public void onclick(view v) {
string value=num.gettext().tostring();
//trim()判断前后是否有空格
if (value== null ||value.trim().equals( "" )){
num.seterror( "请输入内容!!" );
return ;
}
}
});
button2.setonclicklistener( new view.onclicklistener() {
@override
public void onclick(view v) {
string value=abc.gettext().tostring();
//trim()判断前后是否有空格
if (value== null ||value.trim().equals( "" )){
abc.seterror( "请输入内容!!" );
return ;
}
}
});
button3.setonclicklistener( new view.onclicklistener() {
@override
public void onclick(view v) {
string value=num2.gettext().tostring();
//trim()判断前后是否有空格
if (value== null ||value.trim().equals( "" )){
num2.seterror( "请输入内容!!" );
return ;
}
}
});
button4.setonclicklistener( new view.onclicklistener() {
@override
public void onclick(view v) {
string value=email.gettext().tostring();
//trim()判断前后是否有空格
if (value== null ||value.trim().equals( "" )){
email.seterror( "请输入内容!!" );
return ;
}
}
});
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.main, menu);
return true ;
}
}
|
希望本文所述对大家android程序设计有所帮助。