`
ruirui516888
  • 浏览: 136854 次
文章分类
社区版块
存档分类
最新评论

EditText操作收集

 
阅读更多

1.android EditText插入字符串到光标所在位置

EditText mTextInput=(EditText)findViewById(R.id.input);//EditText对象

int index = mTextInput.getSelectionStart();//获取光标所在位置

String text="I want to input str";

Editable edit = mTextInput.getEditableText();//获取EditText的文字

if (index < 0 || index >= edit.length() ){

edit.append(text);

}else{

edit.insert(index,text);//光标所在位置插入文字

}

2.在EditText移动光标

我自己设计了一个拨号键盘,用来模拟android的手机拨号键,自己画了的键盘,显示的控件时editText。 但是点拨号键时光标不能随之输入的字符后移,而是一直呆在最前面。在看了android的文档关于edittext以及它的父类都没有看到相应的解决方法。
最后在google中搜索到一个解决方法。


EditText inputField = new EditText(this);

Editable etext = inputField.getText();

int position = etext.length();

Selection.setSelection(etext, position);

最后可以将光标移动到EditText文本的右边。



3.EditText始终不弹出软件键盘

1.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>
方法二:
让EditText失去焦点,使用EditText的clearFocus方法
例如:EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
方法三:
强制隐藏Android输入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

2.EditText始终不弹出软件键盘
例:EditText edit=(EditText)findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);



4.三种方式限制EditText的输入字数[转]

方法一:利用TextWatcher

editText.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
private boolean isEdit = true;
private int selectionStart ;
private int selectionEnd ;
@Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
temp = s;
}

@Override
public void onTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
}

@Override
public void afterTextChanged(Editable s) {
selectionStart = editText.getSelectionStart();
selectionEnd = editText.getSelectionEnd();
Log.i("gongbiao1",""+selectionStart);
if (temp.length() > Constant.TEXT_MAX) {
Toast.makeText(KaguHomeActivity.this,
R.string.edit_content_limit, Toast.LENGTH_SHORT)
.show();
s.delete(selectionStart-1, selectionEnd);
int tempSelection = selectionStart;
editText.setText(s);
editText.setSelection(tempSelection);
}
}


});

方法二:利用InputFilter
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)}); //其中100最大输入字数

方法三:在XML中设定

Xml代码
<EditText
.
.
.
Android:maxLength="100"
/>



EditText mTextInput=(EditText)findViewById(R.id.input);//EditText对象

int index = mTextInput.getSelectionStart();//获取光标所在位置

String text="I want to input str";

Editable edit = mTextInput.getEditableText();//获取EditText的文字

if (index < 0 || index >= edit.length() ){

edit.append(text);

}else{

edit.insert(index,text);//光标所在位置插入文字

}

2.在EditText移动光标

我自己设计了一个拨号键盘,用来模拟android的手机拨号键,自己画了的键盘,显示的控件时editText。 但是点拨号键时光标不能随之输入的字符后移,而是一直呆在最前面。在看了android的文档关于edittext以及它的父类都没有看到相应的解决方法。
最后在google中搜索到一个解决方法。


EditText inputField = new EditText(this);

Editable etext = inputField.getText();

int position = etext.length();

Selection.setSelection(etext, position);

最后可以将光标移动到EditText文本的右边。



3.EditText始终不弹出软件键盘

1.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>
方法二:
让EditText失去焦点,使用EditText的clearFocus方法
例如:EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
方法三:
强制隐藏Android输入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

2.EditText始终不弹出软件键盘
例:EditText edit=(EditText)findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);



4.三种方式限制EditText的输入字数[转]

方法一:利用TextWatcher

editText.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
private boolean isEdit = true;
private int selectionStart ;
private int selectionEnd ;
@Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
temp = s;
}

@Override
public void onTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
}

@Override
public void afterTextChanged(Editable s) {
selectionStart = editText.getSelectionStart();
selectionEnd = editText.getSelectionEnd();
Log.i("gongbiao1",""+selectionStart);
if (temp.length() > Constant.TEXT_MAX) {
Toast.makeText(KaguHomeActivity.this,
R.string.edit_content_limit, Toast.LENGTH_SHORT)
.show();
s.delete(selectionStart-1, selectionEnd);
int tempSelection = selectionStart;
editText.setText(s);
editText.setSelection(tempSelection);
}
}


});

方法二:利用InputFilter
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)}); //其中100最大输入字数

方法三:在XML中设定

Xml代码
<EditText
.
.
.
Android:maxLength="100"
/>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics