androidedittext字數
A. android 後面代碼怎麼設置edittext的值
1、EditText輸入的文字為密碼形式的設置
(1)通過.xml里設置:
把該EditText設為:android:password="true" // 以」.」形式顯示文本
(2)在代碼里設置:
通過設置EditText的setTransformationMethod()方法來實現隱藏密碼或這顯示密碼。
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//設置密碼為不可見。
2、(1)EditText輸入的文字為電話號碼
Android:phoneNumber=」true」 //輸入電話號碼
3、EditText字數限制的設置
(1)在.xml中設置:android:maxLength=「50」
(2)代碼中設置:
editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)});
4、EditText設置字體
android:typeface="monospace" //設置字型。字形有:normal, sans, serif,monospace
5、EditText是否可編輯
Android:editable // 是否可編輯
6、在EditText中軟鍵盤的調起、關閉
(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); //關閉軟鍵盤
return false;
}
});
(2)當EidtText無焦點(focusable=false)時阻止輸入法彈出
InputMethodManager imm =
(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(),0);
(3)調用數字鍵盤並設置輸入類型和鍵盤為英文
etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //調用數字鍵盤
rlEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);//設置輸入類型和鍵盤為英文 或者:android:inputType="textUri|textMultiLine"
(4)android:focusable="false"//鍵盤永遠不會彈出
<activity android:name=".AddLinkman"android:windowSoftInputMode="adjustUnspecified|stateHidden"/>//不自動彈出鍵盤
//關閉鍵盤(比如輸入結束後執行) 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的區別
(1)android:layout_gravity是本元素對父元素的重力方向。
(2)android:gravity是本元素所有子元素的重力方向。
8、android:padding和android:layout_margin區別
這兩個都可以設置邊距,但有細微的區別:
(1)android:padding是相對父view的邊距
(2)android:layout_margin是相對同一級View的邊距
例:LinearLayout是水平布局,下面有兩個按鈕,
(a)如果右邊的按鈕想距左邊的按鈕15px,因為這兩個按鈕是同一級的,應該用android:layout_margin;
(b)如果右邊的按鈕想距左邊的距離為350px,應該用android:padding
9、android:numeric//只接受數字
android:numeric來控制輸入的數字類型,一共有三種分別為integer(正整數)、signed(帶符號整數,有正負)和decimal(浮點數)。
B. Android EditText當輸入文字換行後,如何讓EditText的高度也隨之適應整個文字的高度呢
我也遇到同樣問題,以下是我解決問題方法,僅供參考。
默認EditText設置warp-content就可以,但是我一直不可以,最終發現是布局有問題,因為在editText中設置了gravity="center_vertical",所以一直被截一半,導致顯示不全,後來我改成了layout_gravity="center_vertical" 就可以了,這是我全部的代碼,我設置了最多字數,你可以不限制
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="@color/white">
<TextView
style="@style/style_left_title_light_color"
android:layout_width="80dp"
android:layout_height="45dp"
android:text="備注" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:background="@color/white"
android:textSize="16dp"
android:textColor="@color/colorPayBtn"
android:textColorHint="@color/colorLightGray"
android:hint="請填寫備注"
android:maxLength="50" />
</LinearLayout>
style:
<style name="style_left_title_light_color">
<item name="android:layout_height">match_parent</item>
<item name="android:gravity">center_vertical</item>
<item name="android:textSize">16dp</item>
<item name="android:textColor">@color/gray</item>
</style>
C. android中textview如何限制字數
一開始採用的方法是函數textView:shouldChangeTextInRange:replacementText:來進行判斷:
[c-sharp] view plain
//鍵入Done時,插入換行符,然後執行addBookmark
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
//判斷加上輸入的字元,是否超過界限
NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
if (str.length > BOOKMARK_WORD_LIMIT)
{
textView.text = [textView.text substringToIndex:BOOKMARK_WORD_LIMIT];
return NO;
}
return YES;
}
但在使用中發現該方法在有聯想輸入的時候,根本無法對聯想輸入的詞進行判斷,然後嘗試使用textViewDidChange:,驗證可行:
[cpp] view plain
/*由於聯想輸入的時候,函數textView:shouldChangeTextInRange:replacementText:無法判斷字數,
因此使用textViewDidChange對TextView裡面的字數進行判斷
*/
- (void)textViewDidChange:(UITextView *)textView
{
//該判斷用於聯想輸入
if (textView.text.length > BOOKMARK_WORD_LIMIT)
{
textView.text = [textView.text substringToIndex:BOOKMARK_WORD_LIMIT];
}