简体   繁体   中英

Can't get EditText to get focus and popup keyboard

I am not able to set the focus on the EditText box at the end. I have tried almost every trick on google! I am not able to get it to focus nor am I able to bring the keyboard.

I have also tried setting the android:windowSoftInputMode="adjustResize" but this also didn't solve the problem.

Please help

<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:textIsSelectable="true"

This line in EditText is causing the problem. Remove it and it should work.

The soft keyboards in Android are highly unpredictable. There are several problems related to editText focus and showing the keyboard. Try these methods to show or hide keyboard programmatically in your app

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM