Android編程開發(fā)之EditText中不輸入特定字符會顯示相關(guān)提示信息的方法
本文實(shí)例講述了Android編程開發(fā)之EditText中不輸入特定字符會顯示相關(guān)提示信息的方法。分享給大家供大家參考,具體如下:
先看效果圖:

源碼如下:
布局文件:
<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="確認(rèn)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="確認(rèn)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="確認(rèn)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="確認(rèn)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:
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("請輸入內(nèi)容?。?);
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("請輸入內(nèi)容?。?);
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("請輸入內(nèi)容??!");
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("請輸入內(nèi)容!!");
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程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android?IdleHandler基本使用及應(yīng)用案例詳解
這篇文章主要為大家詳細(xì)介紹了Android?IdleHandler的基本使用及應(yīng)用案例,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以參考一下2022-10-10
Android 出現(xiàn):java.lang.NoClassDefFoundError...錯誤解決辦法
這篇文章主要介紹了Android 出現(xiàn):Android出現(xiàn):java.lang.NoClassDefFoundError: android/os/PersistableBundle錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android中ExpandableListView的用法實(shí)例
這篇文章主要介紹了Android中ExpandableListView的用法,以實(shí)例形式展示了Android中的下拉list控件的用法,需要的朋友可以參考下2014-10-10
Android 7.0 監(jiān)聽網(wǎng)絡(luò)變化的示例代碼
這篇文章主要介紹了Android 7.0 監(jiān)聽網(wǎng)絡(luò)變化的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Android Activity中使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)與參數(shù)傳遞的方法
這篇文章主要介紹了Android Activity中使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)與參數(shù)傳遞的方法,結(jié)合實(shí)例形式簡單分析了Android中的Activity交互操作相關(guān)技巧,需要的朋友可以參考下2016-07-07
解決Android popupWindow設(shè)置背景透明度無效的問題
這篇文章主要介紹了解決Android popupWindow設(shè)置背景透明度無效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Android簽名文件轉(zhuǎn)化為pk8和pem的實(shí)現(xiàn)
這篇文章主要介紹了Android簽名文件轉(zhuǎn)化為pk8和pem的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

