概述
EditText是TextView的子類,因此TextView的一切xml屬性同樣也適用于EditText.
EidtText
像QQ一樣輸入表情圖片
Demo:
public class InputTextAndPicAct extends Activity {private EditText et_input;
private Button btn_insert;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_input_text_and_pic);initView();}
private void initView() {et_input = (EditText) findViewById(R.id.id_et_inputMess);btn_insert = (Button) findViewById(R.id.id_btn_insertPic);}
/*** 在xml中配置了android:onClick屬性,監聽點擊事件** @param view*/public void insertPic(View view) {
int random =
1 +
new Random().nextInt(
10);LogUtils.d(
"隨機產生的整數:" + random);
try {Field field = R.drawable.class.getField(
"gur_project_" + random);
int resourceId = Integer.parseInt(field.get(
null).toString());Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);ImageSpan imageSpan =
new ImageSpan(
this,bitmap);String text =
"gur_project_";SpannableString spannableString =
new SpannableString(text);spannableString.setSpan(imageSpan,
0,text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);et_input.append(spannableString);}
catch (NoSuchFieldException e) {e.printStackTrace();}
catch (IllegalAccessException e) {e.printStackTrace();}}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000"><EditText
android:id="@+id/id_et_inputMess"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="#FFF"android:minLines="4"android:gravity="left|top"android:padding="10dp" /><Button
android:id="@+id/id_btn_insertPic"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/id_et_inputMess"android:layout_margin="10dp"android:background="#FFF"android:padding="10dp"android:onClick="insertPic"android:text="隨機插入表情" /></RelativeLayout>
在<EditText>標簽中我們設置了android:gravity的屬性為left|top,以便輸入的文本從左上角開始顯示。因為設置了最小顯示4行,如果不設置改屬性,會從左側中心位置開始顯示,如果是單行就不會存在這個問題了。
我們將<Button>標簽的android:onClick屬性指定了單擊事件的方法insertPic,在該方法中,隨機獲取gur_project_1到gur_project_10的任意一個資源的ID, 最常用的作法是將這個10個圖片的資源ID放到一個數據里面,然后隨機產生一個數組索引獲取相應的資源ID,但是我們并沒有采用這種方式,而是采用了直接通過反射技術從R.drawable類中獲取圖像資源ID的方法。 這種方法的好處是一旦圖像非常的多,可以不需要在數組中逐個定義就可以獲取到對應的資源ID了。
我們使用ImageSpan類來直接插入圖像,當然了我們也可以采用<img>標簽在EditText控件中插入圖片,只是復雜一些,本案例沒有采用。
注意事項:
由于R.drawable類中的資源ID都是public 的靜態變量,因此我們可以直接使用Field.get方法獲取這些變量的值。 如果是private或者protected類型的變量,需要field.setAccessible(true)來獲取訪問權限。使用Field.get方法獲取變量時,如果是靜態變量,Field.get方法的參數可以設置為null即可,如果不是靜態變量,則需要為Field.get方法指定一個變量所在類的對象作為參數值。使用EditText類不能直接插入Span對象,因此需要先使用SpannableString對象來封裝Span對象(例如本例中的ImageSpan對象),再將SpannableString對象插入到EditText控件中。
在EditText中輸入特定的字符
EditText控件中可以通過多種方式指定允許輸入的字符,比如指向輸入0~9的數字
三種方式:
- android:digits屬性設置為0123456789
- android:inputType屬性設置為number
- android:numeric設置為integer
<?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:background=
"#000"android:orientation=
"vertical"android:padding=
"10dp"><TextViewandroid:layout_width=
"match_parent"android:layout_height=
"wrap_content"android:layout_margin=
"10dp"android:background=
"#FFF"android:text=
"使用android:digits屬性(輸入數字)" /><EditTextstyle=
"@style/bg_white"android:layout_width=
"match_parent"android:layout_height=
"wrap_content"android:digits=
"0123456789" /><TextViewandroid:layout_width=
"match_parent"android:layout_height=
"wrap_content"style=
"@style/bg_white"android:text=
"使用adnroid:digits屬性輸入26個英文小寫字母"/><EditTextandroid:layout_width=
"match_parent"android:layout_height=
"wrap_content"style=
"@style/bg_white"android:digits=
"abcdefghijklmnopqrstuvwxyz"/><TextViewandroid:layout_width=
"match_parent"android:layout_height=
"wrap_content"android:layout_margin=
"10dp"android:background=
"#FFF"android:text=
"使用android:inputType屬性(輸入數字)" /><EditTextstyle=
"@style/bg_white"android:layout_width=
"match_parent"android:layout_height=
"wrap_content"android:inputType=
"number" /><TextViewandroid:layout_width=
"match_parent"android:layout_height=
"wrap_content"android:layout_margin=
"10dp"android:background=
"#FFF"android:text=
"使用android:inputType屬性(輸入Email)" /><EditTextstyle=
"@style/bg_white"android:layout_width=
"match_parent"android:layout_height=
"wrap_content"android:inputType=
"textEmailAddress" /></LinearLayout><!--
android
1.5以后添加了軟件虛擬鍵盤的功能,所以在輸入提示中將會有對應的軟鍵盤模式
android中inputType屬性在EditText輸入值時啟動的虛擬鍵盤的風格有著重要的作用。這也大大的方便的操作。有時需要虛擬鍵盤只為字符或只為數字。所以inputType尤為重要。
<EditText android:layout_width=
"fill_parent" android:layout_height=
"wrap_content" android:inputType=
"phone" />
//文本類型,多為大寫、小寫和數字符號。
android:inputType=
"none"
android:inputType=
"text"
android:inputType=
"textCapCharacters" 字母大寫
android:inputType=
"textCapWords" 首字母大寫
android:inputType=
"textCapSentences" 僅第一個字母大寫
android:inputType=
"textAutoCorrect" 自動完成
android:inputType=
"textAutoComplete" 自動完成
android:inputType=
"textMultiLine" 多行輸入
android:inputType=
"textImeMultiLine" 輸入法多行(如果支持)
android:inputType=
"textNoSuggestions" 不提示
android:inputType=
"textUri" 網址
android:inputType=
"textEmailAddress" 電子郵件地址
android:inputType=
"textEmailSubject" 郵件主題
android:inputType=
"textShortMessage" 短訊
android:inputType=
"textLongMessage" 長信息
android:inputType=
"textPersonName" 人名
android:inputType=
"textPostalAddress" 地址
android:inputType=
"textPassword" 密碼
android:inputType=
"textVisiblePassword" 可見密碼
android:inputType=
"textWebEditText" 作為網頁表單的文本
android:inputType=
"textFilter" 文本篩選過濾
android:inputType=
"textPhonetic" 拼音輸入
//數值類型
android:inputType=
"number" 數字
android:inputType=
"numberSigned" 帶符號數字格式
android:inputType=
"numberDecimal" 帶小數點的浮點格式
android:inputType=
"phone" 撥號鍵盤
android:inputType=
"datetime" 時間日期
android:inputType=
"date" 日期鍵盤
android:inputType=
"time" 時間鍵盤
android:hint=
"" 是editText的一個提示 如:editText顯示提示:請輸入帳號! 當點擊editText時這個文字會消失。-->
當界面后面的EditText控件中的輸入文本的時候會彈出系統軟鍵盤,并且整個界面會上衣,以便顯示當前正處于焦點的EditText,如果處于某種需要,不想讓界面上移,可以使用getWindows().setSoftInputMode()將輸入法模式設置為WindowManager,LayoutParamas.SOFT_INPUT_ADJSUT_RESIZE.
getWindow()
.setSoftInputMode(WindowManager
.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
AutoCompleteTextView和MultiAutoCompleteTextView
AutoCompleteTextView是對EditText的擴展,它的父類是EditText.
使用AutoCompleteTextView標簽為AutoCompleteTextView設置adapter
public class AutoCompleteTextViewAct extends Activity {private AutoCompleteTextView actv ;
private MultiAutoCompleteTextView mActv;
private String[] array =
new String[]{
"周杰倫",
"周公舉",
"周恩來",
"Google",
"Google Map",
"Google Android",
"Java"};;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_auto_complete_text_view);initView();initViewMulti();}
private void initView() {actv = (AutoCompleteTextView)findViewById(R.id.id_actv);ArrayAdapter<String> adapter =
new ArrayAdapter<String>(
this,android.R.layout.simple_dropdown_item_1line,array);actv.setAdapter(adapter);}
private void initViewMulti() {mActv = (MultiAutoCompleteTextView) findViewById(R.id.id_actv_multi);ArrayAdapter<String> adapter =
new ArrayAdapter<String>(
this,android.R.layout.simple_dropdown_item_1line,array);mActv.setAdapter(adapter);mActv.setTokenizer(
new MultiAutoCompleteTextView.CommaTokenizer());}}
總結
以上是生活随笔為你收集整理的EidtText的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。