edtext 从右边开始输入 安卓_Android开发之EditText属性详解
1、EditText輸入的文字為密碼形式的設(shè)置
(1)通過.xml里設(shè)置:
把該EditText設(shè)為:android:password="true"?// 以”.”形式顯示文本
(2)在代碼里設(shè)置:
通過設(shè)置EditText的setTransformationMethod()方法來實現(xiàn)隱藏密碼或這顯示密碼。
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//設(shè)置密碼為不可見。
2、(1)EditText輸入的文字為電話號碼
Android:phoneNumber=”true”??//輸入電話號碼
3、EditText字?jǐn)?shù)限制的設(shè)置
(1)在.xml中設(shè)置:android:maxLength=“50”
(2)代碼中設(shè)置:
editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)});
4、EditText設(shè)置字體
android:typeface="monospace" //設(shè)置字型。字形有:normal, sans, serif,monospace
5、EditText是否可編輯
Android:editable // 是否可編輯
6、在EditText中軟鍵盤的調(diào)起、關(guān)閉
(1)EditText有焦點(focusable為true)阻止輸入法彈出
editText=(EditText)findViewById(R.id.txtBody);
editText.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
editText.setInputType(InputType.TYPE_NULL); //關(guān)閉軟鍵盤
return false;
}
});
(2)當(dāng)EidtText無焦點(focusable=false)時阻止輸入法彈出
InputMethodManager imm =
(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(),0);
(3)調(diào)用數(shù)字鍵盤并設(shè)置輸入類型和鍵盤為英文
etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //調(diào)用數(shù)字鍵盤
rlEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);//設(shè)置輸入類型和鍵盤為英文 或者:android:inputType="textUri|textMultiLine"
(4)android:focusable="false"//鍵盤永遠不會彈出
//不自動彈出鍵盤
//關(guān)閉鍵盤(比如輸入結(jié)束后執(zhí)行) InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(etEditText.getWindowToken(), 0);
//自動彈出鍵盤
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
etEditText.requestFocus();//讓EditText獲得焦點,但是獲得焦點并不會自動彈出鍵盤
7、android:layout_gravity和android:gravity的區(qū)別
(1)android:layout_gravity是本元素對父元素的重力方向。
(2)android:gravity是本元素所有子元素的重力方向。
8、android:padding和android:layout_margin區(qū)別
這兩個都可以設(shè)置邊距,但有細微的區(qū)別:
(1)android:padding是相對父view的邊距
(2)android:layout_margin是相對同一級View的邊距
例:LinearLayout是水平布局,下面有兩個按鈕,
(a)如果右邊的按鈕想距左邊的按鈕15px,因為這兩個按鈕是同一級的,應(yīng)該用android:layout_margin;
(b)如果右邊的按鈕想距左邊的距離為350px,應(yīng)該用android:padding
9、android:numeric//只接受數(shù)字
android:numeric來控制輸入的數(shù)字類型,一共有三種分別為integer(正整數(shù))、signed(帶符號整數(shù),有正負(fù))和decimal(浮點數(shù))。
10、Enter鍵圖標(biāo)的設(shè)置
軟鍵盤的Enter鍵默認(rèn)顯示的是“完成”文本,我們知道按Enter建表示前置工作已經(jīng)準(zhǔn)備完畢了,要去什么什么啦。比如,在一個搜索中,我們輸入要搜索的文本,然后按Enter表示要去搜索了,但是默認(rèn)的Enter鍵顯示的是“完成”文本,看著不太合適,不符合搜索的語義,如果能顯示“搜索”兩個字或者顯示一個表示搜索的圖標(biāo)多好。事實證明我們的想法是合理的,Android也為我們提供的這樣的功能。通過設(shè)置android:imeOptions來改變默認(rèn)的“完成”文本。這里舉幾個常用的常量值:
(1)actionUnspecified未指定,對應(yīng)常量EditorInfo.IME_ACTION_UNSPECIFIED效果:
(2)actionNone 沒有動作,對應(yīng)常量EditorInfo.IME_ACTION_NONE效果:
(3)actionGo去往,對應(yīng)常量EditorInfo.IME_ACTION_GO 效果:
(4)actionSearch 搜索,對應(yīng)常量EditorInfo.IME_ACTION_SEARCH效果:
(5)actionSend 發(fā)送,對應(yīng)常量EditorInfo.IME_ACTION_SEND效果:
(6)actionNext 下一個,對應(yīng)常量EditorInfo.IME_ACTION_NEXT效果:
(7)actionDone 完成,對應(yīng)常量EditorInfo.IME_ACTION_DONE效果:
11、使用android:imeOptinos可對Android自帶的軟鍵盤進行一些界面上的設(shè)置:
android:imeOptions="flagNoExtractUi"?//使軟鍵盤不全屏顯示,只占用一部分屏幕 同時,這個屬性還能控件軟鍵盤右下角按鍵的顯示內(nèi)容,默認(rèn)情況下為回車鍵 android:imeOptions="actionNone"?//輸入框右側(cè)不帶任何提示 android:imeOptions="actionGo"???//右下角按鍵內(nèi)容為'開始' android:imeOptions="actionSearch"?//右下角按鍵為放大鏡圖片,搜索 android:imeOptions="actionSend"???//右下角按鍵內(nèi)容為'發(fā)送' android:imeOptions="actionNext"??//右下角按鍵內(nèi)容為'下一步' android:imeOptions="actionDone"?//右下角按鍵內(nèi)容為'完成'
12、限定edittext能輸入數(shù)字和字母,并且默認(rèn)輸入為數(shù)字,如身份證號碼
android:inputType="number" android:digits="0123456789xyzXYZ"
13、軟鍵盤的調(diào)起導(dǎo)致原來的界面被擠上去,或者導(dǎo)致界面下面的tab導(dǎo)航被擠上去,解決方法如下
解決方法:
使用Manifest中的Activity的android:windowSoftInputMode的"adjustPan"屬性。
另外注意:有關(guān)軟鍵盤的問題可參考android:windowSoftInputMode中屬性。
14、edittext光標(biāo)詳解?edittext.requestFocusFromTouch();//讓光標(biāo)放入到點擊位置。 edittext.requestFocus();//默認(rèn)方式獲得焦點
EditText editor = (EditText)getCurrentView();//光標(biāo)處插入 int cursor = editor.getSelectionStart(); editor.getText().insert(cursor,delta);
讓光標(biāo)移到末端(這樣文字就會向前顯示) EditText et = ... String text = "text"; et.setText(text); et.setSelection(text.length());
android:cursorVisible="false" 隱藏光標(biāo)
android:background="#00000000"//不要文本框背景
有時候也要對intent的默認(rèn)焦點進行設(shè)置,不至于在intent跳轉(zhuǎn)的時候默認(rèn)焦點(光標(biāo))在EditText上,導(dǎo)致進入intent就打開輸入法,影響界面美觀。
默認(rèn)焦點的順序是:從上倒下
從左到右第一個可以輸入的控件作為焦點
可以使用:
button.setFocusable(true);
button.requestFocus();
button.setFocusableInTouchMode(true);
也可以:
在EditText前面放置一個看不到的LinearLayout,讓他率先獲取焦點:
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
Android?EditText?屬性匯總
Android EditText 屬性匯總
android:layout_gravity="center_vertical" 設(shè)置控件顯示的位置:默認(rèn)top,這里居中顯示,還有bottom android:hint="請輸入數(shù)字!"
設(shè)置顯示在空間上的提示信息 android:numeric="integer" 設(shè)置只能輸入整數(shù),如果是小數(shù)則是:
decimal android:singleLine="true" 設(shè)置單行輸入,一旦設(shè)置為true,則文字不會自動換行。
android:password="true" 設(shè)置只能輸入密碼
android:textColor = "#ff8c00" 字體顏色
android:textStyle="bold" 字體,bold, italic, bolditalic android:textSize="20dip" 大小
android:capitalize = "characters" 以大寫字母寫
android:textAlign="center" EditText沒有這個屬性,但TextView有 android:textColorHighlight="#cccccc" 被選中文字的底色,默認(rèn)為藍色
android:textColorHint="#ffff00" 設(shè)置提示信息文字的顏色,默認(rèn)為灰色 android:textScaleX="1.5" 控制字與字之間的間距
android:typeface="monospace" 字型,normal, sans, serif, monospace android:background="@null" 空間背景,這里沒有,指透明
android:layout_weight="1" 權(quán)重,控制控件之間的地位,在控制控件顯示的大小時蠻有用的。
android:textAppearance="?android:attr/textAppearanceLargeInverse" 文字外觀,這里引用的是系統(tǒng)自帶的一個外觀,?表示系統(tǒng)是否有這種外觀,否則使用默認(rèn)的外觀。不知道這樣理解對不對? 通過EditText的layout xml文件中的相關(guān)屬性來實現(xiàn):
1. 密碼框?qū)傩?android:password="true" 這條可以讓EditText顯示的內(nèi)容自動為星號,輸入時內(nèi)容會在1秒內(nèi)變成*字樣。
2. 純數(shù)字 android:numeric="true" 這條可以讓輸入法自動變?yōu)閿?shù)字輸入鍵盤,同時僅允許0-9的數(shù)字輸入
3. 僅允許 android:capitalize="cwj1987" 這樣僅允許接受輸入cwj1987,一般用于密碼驗證 下面是一些擴展的風(fēng)格屬性
android:editable="false" 設(shè)置EditText不可編輯
android:singleLine="true" 強制輸入的內(nèi)容在單行
android:ellipsize="end" 自動隱藏尾部溢出數(shù)據(jù),一般用于文字內(nèi)容過長一行無法全部顯示時
實現(xiàn)在EditText中輸入自定義表情,需要將放在drawable中的表情圖片,嵌入到EditText中,這里利用ImageSpan實現(xiàn)該功能。
第一步:利用反射機制,得到圖片資源的ID,生成圖片bitmap對象
Field field = R.drawable.class.getDeclaredField("image" + random);
int resouceId = Integer.parseInt(field.get(null).toString());
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resouceId);
第二步:創(chuàng)建imageSpan對象,將imageSpan對象加載到spannableString中
ImageSpan span = new ImageSpan(getApplicationContext(), bitmap);
SpannableString spanna = new SpannableString("image");
spanna.setSpan(span, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
第三步:將SpannableString對象加到EditText中
edittext.append(spanna);
如何在EditText中限定輸入字符的方式:
1.通過設(shè)置digits屬性限定輸入的字符:android:digits="0123456789"
2.通過設(shè)置inputtype屬性選擇輸入字符類型:android:inputType="number"
3.通過設(shè)置numberic屬性接受數(shù)字輸入:android:numeric="integer"
android:inputType來設(shè)置文本的類型,讓輸入法選擇合適的軟鍵盤的.
android:numeric來控制輸入的數(shù)字類型,一共有三種分別為integer(正整數(shù))、signed
(帶符號整數(shù))和decimal(浮點數(shù)).
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
總結(jié)
以上是生活随笔為你收集整理的edtext 从右边开始输入 安卓_Android开发之EditText属性详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 潞安环能是做什么的
- 下一篇: boot spring 接口接收数据_基