活动启动时软键盘不显示 - 更新?

发布于 2024-11-09 16:13:48 字数 3166 浏览 3 评论 0原文

我希望在我的活动开始时打开键盘。我已经尝试了以下问题的所有方法/答案,但没有成功。

我认为问题是当硬件键盘可用时,默认行为是不显示软键盘。这可以被覆盖吗?如果隐藏硬件键盘会发生什么?

我读过以下问题但没有运气。最接近我遇到的问题是在这里: 问题 2712822

其他包括:
问题 3379403
问题 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">
            <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>

我的主要活动代码如下所示:

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);
    */    
    }
}

这是丢失的吗原因?有人可以在带有关闭的硬件键盘的手机上进行测试并告诉我会发生什么吗?

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">
            <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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无力看清 2024-11-16 16:13:48

softinputMode 相关的答案应该有效。如果您无法在清单中设置它,您可以尝试在 onCreate() 中设置它,以指示导航到 Activity 时键盘的显示方式。

要在活动获得焦点时显示它,请使用:

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

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);
靖瑶 2024-11-16 16:13:48

这绝对有效,我一直使用它,不要从 onCreate 调用它,从 onStart 或 onResume 调用它。

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

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);
}
玩套路吗 2024-11-16 16:13:48

试试这个

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

                }
            });

这对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文