前言
上一篇博客介紹了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()轉化獲得,此靜態方法有多種重載模式,可以適應不同的資源來源。
下面直接上代碼,對於布局而言,很簡單的只有兩個控件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <EditText 8 android:id="@+id/edImage" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:layout_marginTop="20dp"/> 12 13 <Button 14 android:id="@+id/btnInImg" 15 android:text="添加表情" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content"/> 18 </LinearLayout>
實現InImageActivity.java代碼:
1 package cn.bgxt.androiduiedittext; 2 3 import java.util.Random; 4 5 import android.app.Activity; 6 import android.graphics.Bitmap; 7 import android.graphics.BitmapFactory; 8 import android.graphics.drawable.Drawable; 9 import android.os.Bundle; 10 import android.text.Spannable; 11 import android.text.SpannableString; 12 import android.text.style.ImageSpan; 13 import android.view.View; 14 import android.widget.Button; 15 import android.widget.EditText; 16 17 public class InImageActivity extends Activity { 18 19 private Button btnInImg; 20 private EditText edImage; 21 //獲取Drawable資源的Id數組 22 private final int[] DRAW_IMG_ID= 23 { 24 R.drawable.image0, 25 R.drawable.image1, 26 R.drawable.image2, 27 R.drawable.image3, 28 R.drawable.image4, 29 R.drawable.image5, 30 R.drawable.image6, 31 R.drawable.image7, 32 R.drawable.image8, 33 R.drawable.image9 34 }; 35 public InImageActivity() { 36 // TODO Auto-generated constructor stub 37 } 38 39 @Override 40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.edittextinimg_activity); 43 44 btnInImg=(Button)findViewById(R.id.btnInImg); 45 edImage=(EditText)findViewById(R.id.edImage); 46 47 btnInImg.setOnClickListener(new View.OnClickListener() { 48 @Override 49 public void onClick(View v) { 50 // 參數一個0-9的隨機數 51 int random=new Random().nextInt(9); 52 //通過bitmapFactory獲得位圖資源 53 Bitmap bit=BitmapFactory.decodeResource(getResources(), DRAW_IMG_ID[random]); 54 //一個ImageSpan,用於插入的存放待插入的圖片 55 ImageSpan imageSpan=new ImageSpan(InImageActivity.this,bit); 56 SpannableString spannableString=new SpannableString("img"); 57 spannableString.setSpan(imageSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 58 edImage.append(spannableString); 59 } 60 }); 61 62 } 63 64 65 66 67 }
顯示效果,點擊按鈕隨機添加標簽:
既然EditText主要是用來獲取用戶輸入的信息的,那么第二個例子就來講講用戶輸入時候內容的驗證吧。在XML Attribute中,有一些屬性可以設置輸入驗證的范圍內容,不過此為TextView類的屬性,因為TextView無法輸入,此處在EditText中講解說明。
- android:digits:指定特定能被輸入的字符。
- android:inputType:設定輸入的類型,下面僅介紹一些常用的,多項可以使用“|”分割。
- textUri:必須是一個URL。
- textEmailAddress:Email地址
- textPassword:密碼。
- number:數字。
- android:numeric:指定數字輸入類型,多項可以使用“|”分割。
- integer:數字。
- decimal:浮點類型。
- signed:帶符號。
以上屬性僅僅是為了限制用戶的輸入,還有一些輸入需要給用戶以提示錯誤信息。這里將使用到setError()方法,如果設定了錯誤提示信息,會在EditText旁邊以感嘆號的形式顯示。
布局代碼:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:text="使用Android:digits屬性(僅輸入數字與abcde)" /> 11 12 <EditText 13 android:id="@+id/etNum" 14 android:layout_width="200dp" 15 android:layout_height="wrap_content" 16 android:layout_margin="10dp" 17 android:digits="123456789abcde" 18 /> 19 <TextView 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="使用Android:inputtype屬性(僅輸入Email)" /> 23 24 <EditText 25 android:layout_width="200dp" 26 android:layout_height="wrap_content" 27 android:layout_margin="10dp" 28 android:inputType="textPassword" 29 /> 30 <TextView 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:text="使用Android:inputtype屬性(僅輸入密碼)" /> 34 35 <EditText 36 android:layout_width="200dp" 37 android:layout_height="wrap_content" 38 android:layout_margin="10dp" 39 android:numeric="decimal|signed" 40 /> 41 <Button 42 android:id="@+id/btnValidation" 43 android:text="驗證第一個輸入框是否為123" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content"/> 46 </LinearLayout>
Java代碼:
1 package cn.bgxt.androiduiedittext; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.EditText; 8 9 public class EditValidationActivity extends Activity { 10 11 private Button btnValidation; 12 private EditText etNum; 13 public EditValidationActivity() { 14 // TODO Auto-generated constructor stub 15 } 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 // TODO Auto-generated method stub 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.editvalidation_activity); 21 btnValidation=(Button)findViewById(R.id.btnValidation); 22 etNum=(EditText)findViewById(R.id.etNum); 23 24 btnValidation.setOnClickListener(new View.OnClickListener() { 25 26 @Override 27 public void onClick(View v) { 28 // TODO Auto-generated method stub 29 String num=etNum.getText().toString().trim(); 30 if(!num.equals("123")) 31 { 32 etNum.setError("請輸入123"); 33 } 34 } 35 }); 36 37 38 } 39 }
效果展示:
如果點擊驗證按鈕,而第一個文本框輸入的不是123,則提示錯誤信息:
總結
以上就講解了EditText在實際項目中常用的效果,雖然大部分使用的是TextView的屬性設置的效果,但是Android下還有一些其他的供用戶輸入的控件,可以使用,所以才以這樣的繼承結構實現屬性。
請支持原創,尊重原創,轉載請注明出處。謝謝。