androidedittext焦点
❶ android中edittex焦点设置和弹不弹出输入法的问题
一:EditText为什么会默认弹出输入法?
同样的代码,碰到有EditText控件的界面时有的机子会弹出输入法,有的机子不会弹出。不好意思,这问题我也一头雾水,谁知道可以告诉我,否则我就把这个问题留下来,以后研究android源码时再搞个清楚。但是...我有解决方案。
二:默认弹出和默认关闭输入法的解决方案。
1.默认关闭,不至于进入Activity就打开输入法,影响界面美观。
①在布局文件中,在EditText前面放置一个看不到的LinearLayout,让他率先获取焦点:
[html] view plainprint?
01.<LinearLayout
02.
03.android:focusable="true"
04.
05.android:focusableInTouchMode="true"
06.
07.android:layout_width="0px"
08.
09.android:layout_height="0px"/>
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
②方法二:先看一个属性android:inputType:指定输入法的类型,int类型,可以用|选择多个。取值可以参考:android.text.InputType类。取值包括:text,textUri,
phone,number,等.
Android SDK中有这么一句话“If
the given content type is TYPE_NULL then a soft keyboard will not be displayed for this text view”,
先将EditText的InputType改变为TYPE_NULL,输入法就不会弹出.然后再设置监听,再重新设置它的InputType.
[java] view plainprint?
01.editText.setOnTouchListener(new OnTouchListener() {
02. public boolean onTouch(View v, MotionEvent event) {
03. // TODO Auto-generated method stub
04. int inType = editText.getInputType(); // backup the input type
05. editText.setInputType(InputType.TYPE_NULL); // disable soft input
06. editText.onTouchEvent(event); // call native handler
07. editText.setInputType(inType); // restore input type
08. return true;
09. }
10. });
editText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int inType = editText.getInputType(); // backup the input type
editText.setInputType(InputType.TYPE_NULL); // disable soft input
editText.onTouchEvent(event); // call native handler
editText.setInputType(inType); // restore input type
return true;
}
});
2.默认弹出。有时候按照需求可能要求默认弹出输入法。方案如下:
[java] view plainprint?
01.EditText titleInput = (EditText) findViewById(R.id.create_edit_title);titleInput.setFocusable(true);
02.
03.titleInput.requestFocus();
04. onFocusChange(titleInput.isFocused());
05.
06.private void onFocusChange(boolean hasFocus)
07.{
08.final boolean isFocus = hasFocus;
09.(new Handler()).postDelayed(new Runnable() {
10.public void run() {
11.InputMethodManager imm = (InputMethodManager)
12.titleInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
13.if(isFocus)
14.{
15.imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
16.}
17.else
18.{
19.imm.hideSoftInputFromWindow(titleInput.getWindowToken(),0);
20.}
21.}
22.}, 100);
23.}
EditText titleInput = (EditText) findViewById(R.id.create_edit_title);titleInput.setFocusable(true);
titleInput.requestFocus();
onFocusChange(titleInput.isFocused());
private void onFocusChange(boolean hasFocus)
{
final boolean isFocus = hasFocus;
(new Handler()).postDelayed(new Runnable() {
public void run() {
InputMethodManager imm = (InputMethodManager)
titleInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(isFocus)
{
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
else
{
imm.hideSoftInputFromWindow(titleInput.getWindowToken(),0);
}
}
}, 100);
}
我觉得因为在Android的主线程中对UI的操作无效,所以必须在Handler中实现弹出输入法的操作
三。关于焦点和输入法的个人理解
获取焦点是获取焦点,弹输入法是弹输入法。获取焦点后并不一定会弹出输入法,在网上搜了一圈,主流回答是“还有就是已开启界面就是focus的text的话有可能也是不行的,UI渲染是需要时间的”......
由于对源码不懂,我对这一点也没有个全面的认识。只能将焦点和输入法分成两块来处理。焦点的打开和关闭特别简单。
焦点的获取:
titleInput.setFocusable(true);
titleInput.requestFocus();
焦点的取消:
titleInput.setFocusable(false);四。关于经常调用的处理软键盘的函数如下:<转载>
1、打开输入法窗口:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 接受软键盘输入的编辑文本或其它视图
imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);
2、关闭出入法窗口
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//接受软键盘输入的编辑文本或其它视图
inputMethodManager
.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);
3、如果输入法打开则关闭,如果没打开则打开
InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
4、获取输入法打开的状态
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
isOpen若返回true,则表示输入法打开
转载
❷ 如何使edittext失去焦点
EditText初始化时候失去焦点:只需要在布局文件中设置属性
android:focusable="false"
也可以在代码中由开发者根据需求设置一定的条件,当条件满足后,动态的设置EditText失去焦点。
示例代码:
EditTextet=(EditText)findViewById(R.id.et);
et.clearFocus();
et.setFocusable(false);
这种控制EditText的操作是Android程序中不经常用到的。多出现于EditText的输入监听回调方法中。
❸ Android中EditText获得焦点后马上又失去了,可能是什么造成的呢
中EditText获得焦点后马上又失去了有以下几种情况:
EditText处在一个可以滚动的控件中,例如ListView等,当得到焦点后软件键盘会弹出,这个时候滚动控件会重绘,因此会失去焦点。
EditText注册了焦点事件,得到焦点后将焦点转向了其它控件。
EditText禁止了获取焦点。
❹ android listview edittext 焦点 滚动
曾经做过这个,但是如果要我敲出代码给你,这个需要比较多的精力。
提供个思路给你:
你可以获取到当前Activity下面的所有View,并且你能判断出Edittext有哪些,还有就是当前Edittext下面的那个Edittext是哪个,这样的话,你按回车 直接在TextWatcher中的onTextChanged中做好处理,当用户按了回车,你就直接让焦点跑到下一个EditText上。
❺ android 自定义的dialog,edit text 不能获得焦点,弹出软键盘。
1、首先自定义布局
2、把自定义布局放入dialog中显示
3、通过自定义布局查找对应的edittext组件
final View DialogView = a .inflate ( R.layout.loand, null);//1、自定义布局
//创建对话框
AlertDialog dlg = new AlertDialog.Builder(loand.this)
.setTitle("登录框")
.setView(DialogView)//设置自定义对话框的样式,2、自定义布局放入dialog中显示
.setPositiveButton("登陆", //设置"确定"按钮
new DialogInterface.OnClickListener() //设置事件监听{
public void onClick(DialogInterface dialog, int whichButton){editText1 =(EditText) DialogView.findViewById(R.id.editText1);
editText2 =(EditText) DialogView.findViewById(R.id.editText2);//3、过自定义布局查找对应的edittext组件
String id = editText1.getText().toString();
String password = editText2.getText().toString();