androidedittext高度
Ⅰ Android中怎樣把EditText的游標放到頂部
在layout文件裡面找到布局文件,那個EditText組件上加一個屬性,android:gravity="top"就可以了,gravity是控制文本的排列方式的,裡面還有好幾種排列方式,你可以自己看看。
如果是在Activity里的話,就在EditText那個文本框對象上調用setGravity(int),通過API查看setGravity(int)方法可以看到int型參數所代表的含義
Ⅱ Android EditText輸入最大長度限制如何給用戶以友好的提示
方法一:
在 xml 文件中設置文本編輯框屬性作字元數限制
如:android:maxLength="10" 即限制最大輸入字元個數為10
方法二:
在代碼中使用InputFilter 進行過濾
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大輸入字元數為20
[java] view plainprint?
01.public class TextEditActivity extends Activity {
02. /** Called when the activity is first created. */
03. @Override
04. public void onCreate(Bundle savedInstanceState) {
05. super.onCreate(savedInstanceState);
06. setContentView(R.layout.main);
07.
08. EditText editText = (EditText)findViewById(R.id.entry);
09. editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
10. }
11.}
public class TextEditActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText = (EditText)findViewById(R.id.entry);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
}
}
方法三:
利用 TextWatcher 進行監聽
[java] view plainprint?
01.package cie.textEdit;
02.
03.import android.text.Editable;
04.import android.text.Selection;
05.import android.text.TextWatcher;
06.import android.widget.EditText;
07.
08./*
09. * 監聽輸入內容是否超出最大長度,並設置游標位置
10. * */
11.public class MaxLengthWatcher implements TextWatcher {
12.
13. private int maxLen = 0;
14. private EditText editText = null;
15.
16.
17. public MaxLengthWatcher(int maxLen, EditText editText) {
18. this.maxLen = maxLen;
19. this.editText = editText;
20. }
21.
22. public void afterTextChanged(Editable arg0) {
23. // TODO Auto-generated method stub
24.
25. }
26.
27. public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
28. int arg3) {
29. // TODO Auto-generated method stub
30.
31. }
32.
33. public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
34. // TODO Auto-generated method stub
35. Editable editable = editText.getText();
36. int len = editable.length();
37.
38. if(len > maxLen)
39. {
40. int selEndIndex = Selection.getSelectionEnd(editable);
41. String str = editable.toString();
42. //截取新字元串
43. String newStr = str.substring(0,maxLen);
44. editText.setText(newStr);
45. editable = editText.getText();
46.
47. //新字元串的長度
48. int newLen = editable.length();
49. //舊游標位置超過字元串長度
50. if(selEndIndex > newLen)
51. {
52. selEndIndex = editable.length();
53. }
54. //設置新游標所在的位置
55. Selection.setSelection(editable, selEndIndex);
56.
57. }
58. }
59.
60.}
package cie.textEdit;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;
/*
* 監聽輸入內容是否超出最大長度,並設置游標位置
* */
public class MaxLengthWatcher implements TextWatcher {
private int maxLen = 0;
private EditText editText = null;
public MaxLengthWatcher(int maxLen, EditText editText) {
this.maxLen = maxLen;
this.editText = editText;
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Editable editable = editText.getText();
int len = editable.length();
if(len > maxLen)
{
int selEndIndex = Selection.getSelectionEnd(editable);
String str = editable.toString();
//截取新字元串
String newStr = str.substring(0,maxLen);
editText.setText(newStr);
editable = editText.getText();
//新字元串的長度
int newLen = editable.length();
//舊游標位置超過字元串長度
if(selEndIndex > newLen)
{
selEndIndex = editable.length();
}
//設置新游標所在的位置
Selection.setSelection(editable, selEndIndex);
}
}
}
對應的 activity 部分的調用為:
[java] view plainprint?
01.package cie.textEdit;
02.
03.import android.app.Activity;
04.import android.os.Bundle;
05.import android.text.InputFilter;
06.import android.widget.EditText;
07.
08.public class TextEditActivity extends Activity {
09. /** Called when the activity is first created. */
10. @Override
11. public void onCreate(Bundle savedInstanceState) {
12. super.onCreate(savedInstanceState);
13. setContentView(R.layout.main);
14.
15. EditText editText = (EditText) findViewById(R.id.entry);
16. editText.addTextChangedListener(new MaxLengthWatcher(10, editText));
17.
18. }
19.}
package cie.textEdit;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.widget.EditText;
public class TextEditActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText = (EditText) findViewById(R.id.entry);
editText.addTextChangedListener(new MaxLengthWatcher(10, editText));
}
}
限制輸入字元數為10個
main.xml 文件
[html] view plainprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:layout_width="fill_parent"
04. android:layout_height="fill_parent">
05. <TextView
06. android:id="@+id/label"
07. android:layout_width="fill_parent"
08. android:layout_height="wrap_content"
09. android:text="Type here:"/>
10. <EditText
11. android:id="@+id/entry"
12. android:singleLine="true"
13. android:layout_width="fill_parent"
14. android:layout_height="wrap_content"
15. android:background="@android:drawable/editbox_background"
16. android:layout_below="@id/label"/>
17. <Button
18. android:id="@+id/ok"
19. android:layout_width="wrap_content"
20. android:layout_height="wrap_content"
21. android:layout_below="@id/entry"
22. android:layout_alignParentRight="true"
23. android:layout_marginLeft="10dip"
24. android:text="OK" />
25. <Button
26. android:layout_width="wrap_content"
27. android:layout_height="wrap_content"
28. android:layout_toLeftOf="@id/ok"
29. android:layout_alignTop="@id/ok"
30. android:text="Cancel" />
31.</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:"/>
<EditText
android:id="@+id/entry"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
Ⅲ android 怎麼判斷edittext
Android中EditText就是文本輸入控制項,它的值是個String類型, 判斷輸入是否為空可以通過String TextUtil 等API來判斷 有以下幾種方式: 直接判斷EditText的長度editText.length() 如果等於0則為空 通過TextUtil.isEmpty(editText.getText()) true表示是空,false表示非空 通過正則表達式 通過String.length() 判斷長度 以下為示例代碼,如果為空,則跳出提示: String txt = editText.getText().toString(); if(txt.length() == 0){ Toast.makeText(context,"輸入不能為空",0).show(); //彈出一個自動消失的提示框 return; }
Ⅳ 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>
Ⅳ android 如何用代碼實現EditText控制項中只能輸入數字
通過設置輸入類型參數 inputType進行限制。 以下例子定義了文本輸入框EditText的id,控制項的寬度和高度,並通過inputType類型限制輸入為number。 這樣使用者點擊該控制項時,會觸發手機原生態介面,並讓鍵盤的默認布局為數字。 <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" />
Ⅵ Android中EditText無法完全顯示
android:layout_alignBottom="@+id/name"
android:layout_alignTop="@+id/name"
這兩句話去掉就好了。網路下一些屬性的用法。
動手敲打碼是編程好的開始。也要學會網路