시작 활동시 자동 팝업 키보드
비교적 간단한 질문이 있습니다. EditText가 많은 활동이 있습니다. 활동을 열면 자동으로 첫 번째 EditText에 초점을 맞추고 가상 키보드를 표시합니다.
어떻게 방지 할 수 있습니까?
XML 파일의 레이아웃 태그에 다음 속성을 사용하십시오.
android:focusable="true"
android:focusableInTouchMode="true"
댓글에서 다른 회원이보고 한대로 작동하지 않으므로 ScrollView
이러한 속성을의 기본 하위 항목에 추가해야합니다 ScrollView
.
이를 Android Manifest 활동에 추가 할 수 있습니다.
android:windowSoftInputMode="stateHidden|adjustResize"
여기에 몇 가지 구현이 설명되어 있지만 이제 속성에 AndroidManifest.xml
대해에 추가 Activity
했습니다.
android:windowSoftInputMode="stateAlwaysHidden"
을 사용하더라도 이것이 쉬운 방법이라고 생각합니다 fragments
.
" stateAlwaysHidden "활동의 기본 창에 입력 포커스가있을 때 소프트 키보드는 항상 숨겨집니다.
와 같은 활동에 대한 다른보기가있는 경우 다음을 ListView
수행 할 수도 있습니다.
ListView.requestFocus();
onResume ()에서 editText
.
이 질문에 대한 답변이 있지만 나를 위해 일한 대체 솔루션을 제공한다는 것을 알고 있습니다. :)
https://stackoverflow.com/a/11627976/5217837 거의 맞습니다.
@Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
그러나 SOFT_INPUT_STATE_ALWAYS_VISIBLE이 아니라 SOFT_INPUT_STATE_HIDDEN이어야합니다.
활동 코드에서 다음을 사용하십시오.
@Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
태블릿에서 Android 3.2.1을 사용하면 탭을 전환 할 때도 키보드가 자동으로 표시되고 계속 표시되는 유사한 문제가 발생했습니다. 다음 방법을 사용하십시오.
public void setEditTextFocus(EditText searchEditText, boolean isFocused)
{
searchEditText.setCursorVisible(isFocused);
searchEditText.setFocusable(isFocused);
searchEditText.setFocusableInTouchMode(isFocused);
if (isFocused) {
searchEditText.requestFocus();
} else {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
}
}
각 EditText에 대한 활동의 onCreate () 및 onPause ()에서 :
setEditTextFocus(myEditText, false);
For each EditText an OnTouchListener:
myEditText.setOnTouchListener(new EditText.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
setEditTextFocus(myEditText, true);
return false;
}
});
For each EditText
in the OnEditorActionListener
:
myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
.......
setEditTextFocus(myEditText, false);
return false;
}
});
And for each EditText
in the layout xml
:
android:imeOptions="actionDone"
android:inputType="numberDecimal|numberSigned" // Or something else
There is probably more code optimizing possible.
((InputMethodManager)getActivity().getSystemService("input_method")).hideSoftInputFromWindow(this.edittxt.getWindowToken(), 0);
I have found this simple solution that worked for me.Set these attributes in your parent layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
And now, when the activity starts this main layout will get focus by default.
Also, we can remove focus from child views at runtime by giving the focus to the main layout again, like this:
findViewById(R.id.mainLayout).requestFocus();
Hope it will work for you .
this is the solution I am using, is not the best solution but it's working well for me
editComment.setFocusableInTouchMode(false);
editComment.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
editComment.setFocusableInTouchMode(true);
editComment.requestFocus() ;
return false;
}});
Interestingly, this documentation https://developer.android.com/training/keyboard-input/visibility.html states that when an activity starts and focus is given to a text field, the soft keyboard is not shown (and then goes on to show you how to have the keyboard shown if you want to with some code).
On my Samsung Galaxy S5, this is how my app (with no manifest entry or specific code) works -- no soft keyboard. However on a Lollipop AVD, a soft keyboard is shown -- contravening the doc given above.
If you get this behavior when testing in an AVD, you might want to test on a real device to see what happens.
This has some good answers at the following post : Stop EditText from gaining focus at Activity startup. The one I regularly use is the following code by Morgan :
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
NOTE : The dummy item has to be PLACED RIGHT BEFORE the focusable element.
And I think it should work perfectly even with ScrollView and haven't had any problems with accessibility either for this.
This occurs when your EditText automatically gets Focus as when you activity starts. So one easy and stable way to fix this, is simply to set the initial focus to any other view, such as a Button etc.
You can do this in your layout XML, no code required..
Accepted answer is not working for me, that's why give answer working solution, may be it is helpful !
EditText edt = (EditText) findViewById(R.id.edt);
edt.requestFocus();
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
Now keyboard is open enjoy :)
android:windowSoftInputMode="stateHidden|adjustResize"
Working fine
Add below code to your top of the activity XML and make sure the View is above EditText
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:focusableInTouchMode="true"/>
android:focusableInTouchMode="true"
Add the above line to xml of EditText
or TextInputLayout
which has focus and is causing the softInputKeyboard
to pop up.
This solved the problem for us and now the keyboard doesn't popup
If your view has EditText and Listview then Keyboard will open up by default. To hide keyboard from popping up by default do the following
this.listView.requestFocus();
Make sure you are requesting focus on listview after getting view for editText.
For e.g.
this.listView = (ListView) this.findViewById(R.id.list);
this.editTextSearch = (EditText) this.findViewById(R.id.editTextSearch);
this.listView.requestFocus();
If you do it, then editText will get focus and keyboard will pop up.
참고URL : https://stackoverflow.com/questions/4668210/automatic-popping-up-keyboard-on-start-activity
'Programing' 카테고리의 다른 글
"호스트를 확인할 수 없습니다. (0) | 2020.08.26 |
---|---|
OpenCart 전문가가되는 방법? (0) | 2020.08.26 |
MySQL에서 BIT와 TINYINT의 차이점은 무엇입니까? (0) | 2020.08.25 |
입력 유형 = 암호, 브라우저가 암호를 기억하지 못하도록합니다. (0) | 2020.08.25 |
PHP json_decode ()는 유효한 JSON으로 NULL을 반환합니까? (0) | 2020.08.25 |