开发者

Soft keyboard does not show when Activity starts - Update?

I would like the keyboard to open when my Activity starts. I've tried all the methods/answers with the questions below with no luck.

I believe the issue is when the hardware keyboard is available, the default behavior is for the soft keyboard to not be displayed. Can this be overridden? What happens if the hardware keyboard is hidden?

I've read the following questions with no luck. The closest to the problem I'm experiencing is here: Question 2712822

Others Include:

Question 3379403

Question 2479504

Main.xml:

<?xml version="1.0" encoding="utf-8"?>
  <ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
         <LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent">
            <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
            <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/testText" android:focusable="true" android:focusableInTouchMode="true" android:hint="Input here"></EditText>
        </LinearLayout>
        <LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent">
            <开发者_C百科;TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
            <EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="and here"></EditText>
        </LinearLayout>  
    </LinearLayout>
 </ScrollView>

My Main Activity code looks like this:

package com.example.example3;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class example3 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText edit = (EditText)this.findViewById(R.id.testText);
    edit.requestFocus();

    /*       // Below Doesn't work
    InputMethodManager imm = (InputMethodManager)
    example3.this.getSystemService(Context.INPUT_METHOD_SERVICE);

    if (imm != null){/          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    }

    //Below Doesn't work
   // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    */    
    }
}

Is this a lost cause? Can someone test this on a phone with hardware keyboard that is closed and tell me what happens?


The softinputMode related answers should be working. If you're having no luck with setting it in the manifest you might try setting it in onCreate() to dictate how the keyboard is shown when navigating to the activity.

To show it anytime the activity gets focus use:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);


This definitley works, i use it all the time, dont call it from onCreate, call it from onStart or onResume.

public static void showKeyboard(Activity act,EditText t){
            InputMethodManager imm = (InputMethodManager)act.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(t, 0);
}


Try this

 edtReceiver.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus)
                    {

                //  getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        if(imm != null)
                        {
                        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
                        }
                    }

                }
            });

This works for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜