Android EditText的設置


1、輸入法Enter鍵圖標的設置:

軟件盤的界面替換只有一個屬性android:imeOptions,這個屬性的可以取的值有normal,actionUnspecified,actionNone,actionGo,actionSearch,actionSend,actionNext,actionDone,例如當值為actionNext時enter鍵外觀變成一個向下箭頭,而值為actionDone時enter鍵外觀則變成了“完成”兩個字。 
我們也可以重寫enter的事件

 

軟鍵盤的Enter鍵默認顯示的是“完成”文本,通過設置android:imeOptions來改變默認的“完成”文本。這里舉幾個常用的常量值:

actionUnspecified  未指定,對應常量EditorInfo.IME_ACTION_UNSPECIFIED.  
actionNone 沒有動作,對應常量EditorInfo.IME_ACTION_NONE 
actionGo 去往,對應常量EditorInfo.IME_ACTION_GO
actionSearch 搜索,對應常量EditorInfo.IME_ACTION_SEARCH    
actionSend 發送,對應常量EditorInfo.IME_ACTION_SEND   
actionNext 下一個,對應常量EditorInfo.IME_ACTION_NEXT   
actionDone 完成,對應常量EditorInfo.IME_ACTION_DONE  

 

(EditorInfo.inputType & EditorInfo.TYPE_CLASS_MASK)可以是許多不同的值,包括: 
TYPE_CLASS_NUMBER 
TYPE_CLASS_DATETIME 
TYPE_CLASS_PHONE 
TYPE_CLASS_TEXT

 

2、事件捕捉處理:

可以通過setOnEditorActionListener設置事件處理。

final EditText input = new EditText(this);   
input.setSingleLine(true); //android:singleLine=”true”  
   input.setImeOptions(EditorInfo.IME_ACTION_SEND);  
   input.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);  
   input.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
       public boolean onEditorAction(TextView v, int actionId,    
               KeyEvent event)  {    
        Log.d(TAG, ""+actionId+","+event);  
           if (actionId==EditorInfo.IME_ACTION_SEND  
                ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) {    
            //do something;  
            return true;  
           }    
           return false;    
       }    
   }); 

 

3、editor密碼隱藏,怎么寫?

有2種方法處理:

代碼方法:input.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);

layout配置方法:android:inputType="textPassword"

 

4、activity加載完成后,edit輸入框會自動彈出輸入法,可以通過以下代碼屏蔽:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

5、設置EditText始終不彈出軟件鍵盤 
例:EditText edit=(EditText)findViewById(R.id.edit); 
       edit.setInputType(InputType.TYPE_NULL);

 

6、讓 EditText失去焦點,使用EditText的clearFocus方法 
例如:EditText edit=(EditText)findViewById(R.id.edit); 
           edit.clearFocus();

 

7、EditText默認不彈出軟件鍵盤

在 AndroidMainfest.xml中選擇activity,設置windowSoftInputMode屬性為 adjustUnspecified|stateHidden

< activity android:name=".Main"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified|stateHidden"
android:configChanges="orientation|keyboardHidden">
< intent-filter>
< action android:name="android.intent.action.MAIN" />
< category android:name="android.intent.category.LAUNCHER" />
< /intent-filter>
< /activity>

 

8、設置光標到指定位置

EditText et = (EditText) findViewById(R.id.etTest);
et.setSelection(2);
//設置光標不顯示,但不能設置光標顏色
et.setCursorVisible(false);

//獲得焦點時全選文本
et.setSelectAllOnFocus(true);

et.requestFocus(); //請求獲取焦點
et.clearFocus(); //清除焦點

使用EditText的setError提示
et.setError("郵箱"); 
 

自定義圖標的setError提示
Drawable dr = getResources().getDrawable(R.drawable.ic_launcher);
dr.setBounds(0, 0, 10, 10); //必須設置大小,否則不顯示
et.setError("有錯誤提示", dr);
 
        

et.setInputType(InputType.TYPE_CLASS_PHONE);//只能輸入電話號碼
et.setInputType(InputType.TYPE_CLASS_NUMBER);
//只能輸入數字
et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);//只能輸入郵箱地址
et.setInputType(InputType.TYPE_NULL); // 禁止輸入(不彈出輸入法)

XML實現案例
<EditText android:id="@+id/etTest" android:inputType="number"
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/>
 
        

 

 
 
        
 
        
 

8、EditText相關屬性

EditText繼承關系:View-->TextView-->EditText。 
EditText的屬性很多,這里介紹幾個: 
android:layout_gravity="center_vertical" 
設置控件顯示的位置:默認 top,這里居中顯示,還有bottom 
android:hint="請輸入數字!" 
設置顯示在空間上的提示信息 
android:numeric="integer" 
設置只能輸入整數,如果是小數則是:decimal 
android:singleLine="true" 
設置單行輸入,一旦設置為true,則文字不會自動換行。 
android:password="true" 
設置只能輸入密碼 
android:textColor = "#ff8c00" 
字體顏色 
android:textStyle="bold" 
字體,bold, italic, bolditalic 
android:textSize="20dip" 
大小 
android:capitalize = "characters" 
以大寫字母寫 
android:textAlign="center" 
EditText沒有這個屬性,但TextView有,居中 
android:textColorHighlight="#cccccc" 
被選中文字的底色,默認為藍色 
android:textColorHint="#ffff00" 
設置提示信息文字的顏色,默認為灰色 
android:textScaleX="1.5" 
控制字與字之間的間距 
android:typeface="monospace" 
字型,normal, sans, serif, monospace 
android:background="@null" 
空間背景,這里沒有,指透明 
android:layout_weight="1" 
權重,控制控件之間的地位,在控制控件顯示的大小時蠻有用的。 
android:textAppearance="?android:attr/textAppearanceLargeInverse"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM