(从http://blog.csdn.net/caoshichao520326/article/details/8536961中看到我需要的,详细请点此处)
Activity中的EditText在activity创建我需要它能够自动弹出软件盘,但现在一直不行,找了半天才找到原因,原来在activity刚创建时,数据还没有加载完全就没有办法弹出软件盘,此时应该适时的延迟软键盘的弹出,如500毫秒(保证界面的的数据加载完成,如果500毫秒仍未弹出,可以适当延迟),可以在EditText后面加上一段代码,实例如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
et.setFocusable(true);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager inputManager = (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(et, 0);
}
}, 500);
}
不知道调了哪些代码,有个界面中有edittext,每当activity oncreat()时,软键盘又突然弹出来了,但这个页面不能让 它 弹出来,
于是找到:(http://blog.csdn.net/howlaa/article/details/39049081;)
只需要在et的父控件(组件)上天机两行
- android:focusable="true"
- android:focusableInTouchMode="true"
-
在scrollview中嵌套EditText时有时打开这个界面会自动获取焦点(软键盘自动打开),打开另一个同样scrollview嵌套Edittext的界面却不会自动获取焦点,后来试了两次,第一个是因为刚打开就能开到editText所以自动获取焦点了,第二个确实editText在下面,需要向上滑动才能看见,所以打开时没有自动获取焦点; 通过查看其它微博,找到解决 的方法:
et_issue_banzu.setFocusable(false); //① et_issue_banzu.setFocusableInTouchMode(false); //② ①和②表示一开始让edittext失去焦点 et_issue_banzu.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (view.getId()) { case R.id.et_issue_job: view.getParent().requestDisallowInterceptTouchEvent(true); et_issue_banzu.setFocusable(true); // et_issue_banzu.setFocusableInTouchMode(true); //点击它的时候获取焦点 switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_UP: //手指抬起时,告诉父亲你来操作吧 view.getParent().requestDisallowInterceptTouchEvent(false); break; } break; } return false; } });
2.在开发中 ,我在editext属性中设置 不让其获取焦点,然后点击 edittext,让它从底部弹出,这是遇到的问题是,我给edittext设置点击事件,在事件中让edittext获取焦点,于是,这样的后果是我点击第一次时,edittext获取焦点,我在点击,它自动弹起。这样的话,我点击两次,它才能弹出键盘。 之所以会这样 ,是因为点击事件手抬起之后执行里面的方法,此时edittext才获取焦点,再点击时获取软件盘。-------解决方案,①:在点击事件中 让edittext获取焦点,并且主动弹出软键盘 ②:使用触摸事件,setOnTouchListener()