软键盘未在android中打开

时间:2016-06-14 06:21:18

标签: android android-edittext android-keypad android-input-method

我是android的新手并且正在开发一个演示应用程序,因为我有一个活动在屏幕顶部包含一个edittext,并希望在该活动开始时显示键盘我尝试了很多方法但没有这项工作,任何研究可以帮助我排序吗?

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 个答案:

答案 0 :(得分:1)

你必须把重点放在EditText.Try

android:focusable="true"
<\ n>在您的EditText中,它将打开软键盘。

尝试将清单更改为

android:windowSoftInputMode="stateVisible"

而不是

android:windowSoftInputMode="stateAlwaysVisible"

如果问题仍然存在,请尝试使用

以编程方式打开它
InputMethodManager imm = (InputMethodManager)getSystemService(
    Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, 0);

您也可以在SO

上提及This answer