无法让 EditText 获得焦点和弹出键盘
Can't get EditText to get focus and popup keyboard
我无法将焦点设置在最后的 EditText
框上。我在 google 上几乎尝试了所有技巧!我无法让它聚焦,也无法带上键盘。
我也试过设置 android:windowSoftInputMode="adjustResize"
但这也没有解决问题。
请帮忙
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@android:color/white"
android:touchscreenBlocksFocus="false">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spnIncidentCategory"
android:layout_alignParentTop="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:spinnerMode="dropdown"
android:clickable="true"
android:background="@drawable/iv_border"
android:padding="10dp"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/textView24"
android:layout_below="@+id/textView24" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/drop_down"
android:layout_alignParentTop="false"
android:layout_alignTop="@+id/spnIncidentCategory"
android:id="@+id/imgSpinner1"
android:focusableInTouchMode="false"
android:background="@drawable/border"
android:layout_alignEnd="@id/spnIncidentCategory" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/pbSendInfo"
android:visibility="invisible"
android:indeterminate="true"
android:progressTint="#ffffffff"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"
android:layout_alignParentBottom="false" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtPlaceHolder"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ff000000"
android:layout_below="@+id/spnIncidentCategory" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Select an Incident Category"
android:id="@+id/textView24"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="true"
android:textColor="#000000"
android:layout_below="@+id/pbSendInfo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Select an Incident Description"
android:id="@+id/textView25"
android:textColor="#000000"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/txtPlaceHolder" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spnIncidentDescription"
android:focusable="true"
android:focusableInTouchMode="true"
android:spinnerMode="dropdown"
android:clickable="true"
android:background="@drawable/iv_border"
android:padding="10dp"
android:layout_alignStart="@+id/spnIncidentCategory"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView25"
android:layout_alignLeft="@+id/textView25" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtPlaceHolder2"
android:textColor="#ff000000"
android:layout_below="@+id/spnIncidentDescription"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/drop_down"
android:id="@+id/imgSpinner2"
android:focusableInTouchMode="false"
android:background="@drawable/border"
android:layout_alignTop="@+id/spnIncidentDescription"
android:layout_alignEnd="@id/spnIncidentDescription" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter any comment that will help resolve the issue faster"
android:id="@+id/textView26"
android:textColor="#000000"
android:layout_below="@+id/txtPlaceHolder2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtPlaceHolder3"
android:textColor="#ff000000"
android:layout_below="@+id/txtUserComments" />
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/btnSendInfo"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_below="@+id/txtPlaceHolder3"
android:src="@mipmap/send_info"
android:background="@drawable/border"
android:layout_marginTop="5dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/txtUserComments"
android:editable="true"
android:textColor="#000000"
android:textIsSelectable="true"
android:clickable="true"
android:padding="5dp"
android:gravity="top"
android:hint="Enter Your Comments"
android:textColorHint="#86acafb9"
android:selectAllOnFocus="true"
android:nestedScrollingEnabled="true"
android:focusableInTouchMode="true"
android:focusable="true"
android:enabled="true"
android:elevation="@dimen/abc_action_bar_default_padding_material"
android:inputType="textImeMultiLine|text|textMultiLine"
android:layout_below="@+id/textView26"
android:elegantTextHeight="true"
android:layout_marginTop="10dp">
<requestFocus/>
</EditText>
</RelativeLayout>
enter code here
Android 中的软键盘非常难以预测。有几个与 editText 焦点和显示键盘相关的问题。尝试这些方法在您的应用程序中以编程方式显示或隐藏键盘
public static void hideKeyboard(FragmentActivity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public static void showKeyboard(FragmentActivity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
}
}
您尝试过以编程方式提供焦点吗?
text.requestFocus();
android:textIsSelectable="true"
EditText
中的这一行导致了问题。删除它,它应该可以工作。
我无法将焦点设置在最后的 EditText
框上。我在 google 上几乎尝试了所有技巧!我无法让它聚焦,也无法带上键盘。
我也试过设置 android:windowSoftInputMode="adjustResize"
但这也没有解决问题。
请帮忙
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@android:color/white"
android:touchscreenBlocksFocus="false">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spnIncidentCategory"
android:layout_alignParentTop="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:spinnerMode="dropdown"
android:clickable="true"
android:background="@drawable/iv_border"
android:padding="10dp"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/textView24"
android:layout_below="@+id/textView24" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/drop_down"
android:layout_alignParentTop="false"
android:layout_alignTop="@+id/spnIncidentCategory"
android:id="@+id/imgSpinner1"
android:focusableInTouchMode="false"
android:background="@drawable/border"
android:layout_alignEnd="@id/spnIncidentCategory" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/pbSendInfo"
android:visibility="invisible"
android:indeterminate="true"
android:progressTint="#ffffffff"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="true"
android:layout_alignParentTop="false"
android:layout_alignParentBottom="false" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtPlaceHolder"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ff000000"
android:layout_below="@+id/spnIncidentCategory" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Select an Incident Category"
android:id="@+id/textView24"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="true"
android:textColor="#000000"
android:layout_below="@+id/pbSendInfo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Select an Incident Description"
android:id="@+id/textView25"
android:textColor="#000000"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/txtPlaceHolder" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spnIncidentDescription"
android:focusable="true"
android:focusableInTouchMode="true"
android:spinnerMode="dropdown"
android:clickable="true"
android:background="@drawable/iv_border"
android:padding="10dp"
android:layout_alignStart="@+id/spnIncidentCategory"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView25"
android:layout_alignLeft="@+id/textView25" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtPlaceHolder2"
android:textColor="#ff000000"
android:layout_below="@+id/spnIncidentDescription"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/drop_down"
android:id="@+id/imgSpinner2"
android:focusableInTouchMode="false"
android:background="@drawable/border"
android:layout_alignTop="@+id/spnIncidentDescription"
android:layout_alignEnd="@id/spnIncidentDescription" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter any comment that will help resolve the issue faster"
android:id="@+id/textView26"
android:textColor="#000000"
android:layout_below="@+id/txtPlaceHolder2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtPlaceHolder3"
android:textColor="#ff000000"
android:layout_below="@+id/txtUserComments" />
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/btnSendInfo"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_below="@+id/txtPlaceHolder3"
android:src="@mipmap/send_info"
android:background="@drawable/border"
android:layout_marginTop="5dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/txtUserComments"
android:editable="true"
android:textColor="#000000"
android:textIsSelectable="true"
android:clickable="true"
android:padding="5dp"
android:gravity="top"
android:hint="Enter Your Comments"
android:textColorHint="#86acafb9"
android:selectAllOnFocus="true"
android:nestedScrollingEnabled="true"
android:focusableInTouchMode="true"
android:focusable="true"
android:enabled="true"
android:elevation="@dimen/abc_action_bar_default_padding_material"
android:inputType="textImeMultiLine|text|textMultiLine"
android:layout_below="@+id/textView26"
android:elegantTextHeight="true"
android:layout_marginTop="10dp">
<requestFocus/>
</EditText>
</RelativeLayout>
enter code here
Android 中的软键盘非常难以预测。有几个与 editText 焦点和显示键盘相关的问题。尝试这些方法在您的应用程序中以编程方式显示或隐藏键盘
public static void hideKeyboard(FragmentActivity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public static void showKeyboard(FragmentActivity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
}
}
您尝试过以编程方式提供焦点吗?
text.requestFocus();
android:textIsSelectable="true"
EditText
中的这一行导致了问题。删除它,它应该可以工作。