Soft keyboard not open in android [duplicate]

别等时光非礼了梦想. 提交于 2019-12-11 04:22:09

问题


I am newbie to android and working on a demo app,In that i am having an activity contains an edittext at the top of the screen and want to show the keyboard on the start of that activity i have tried many ways but none of this works,Can anybudy help me to sort it ot?

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/rl_hdr"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#16BBA1"
        android:gravity="center_vertical"
        android:padding="10dp">

        <ImageView
            android:id="@+id/iv_back"
            android:layout_width="25dp"
            android:layout_height="22dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="0dp"
            android:src="@drawable/ic_back_white" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:textColorHint="#939393"
            android:singleLine="true"
            android:layout_centerInParent="true"
            android:hint="Search"
            android:layout_marginLeft="5dp"
            android:padding="5dp"
            android:gravity="center_vertical"
            android:drawablePadding="5dp"
            android:layout_marginRight="5dp"
            android:textSize="16dp"

            android:imeOptions="actionDone"
            android:cursorVisible="true"
            android:layout_toRightOf="@+id/iv_back"
            android:drawableRight="@drawable/ic_search"
            android:background="@drawable/bg_et_actionbar"
            android:id="@+id/et_search" />


    </RelativeLayout>

       <FrameLayout
            android:id="@+id/fragmentTimeline"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"

            android:layout_marginTop="0dp" />

</LinearLayout>

manifest.xml

<activity
            android:name="one.tusk.stush.SearchPostActivity"
            android:label="Back"
            android:parentActivityName="one.tusk.stush.activities.MainActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateAlwaysVisible" >
            >
        </activity>

SerachPostActivity.java

public class SearchPostActivity extends FragmentActivity implements OnQueryTextListener, OnItemClickListener  {

    public EditText et_search;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.search_post);
.
.
.
.
et_search = (EditText)findViewById(R.id.et_search);
    InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInputFromWindow(linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);

et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                    Log.i("Done pressed", "Enter pressed");
                    search.searchPost(et_search.getText().toString().trim());
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                return false;
            }
        });


        et_search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus){
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(et_search, InputMethodManager.SHOW_FORCED);
                } else {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        });

回答1:


You have to give focus to EditText.Try

android:focusable="true"

in your EditText and it will open the soft keyboard.

Try changing your manifest to

android:windowSoftInputMode="stateVisible"

instead of

android:windowSoftInputMode="stateAlwaysVisible".

If problem persists,try opening it programatically using

InputMethodManager imm = (InputMethodManager)getSystemService(
    Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, 0);

You may also refer This answer on SO



来源:https://stackoverflow.com/questions/37804336/soft-keyboard-not-open-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!