【问题标题】:Android Close/Hide keyboard programmatically in TabHost?在 TabHost 中以编程方式关闭/隐藏键盘?
【发布时间】:2013-01-29 09:32:09
【问题描述】:

我目前正在开发一个 Android 应用程序,当您首次加载此应用程序时,它使用 TabHost 来显示应用程序的各种视图(活动)。因此,当它加载时,它有一个视图,用户可以在其中输入一个数字,以便自动显示软键盘。

起初我假设当用户转到应用程序中的其他选项卡时,如果没有 EditText 字段,键盘会消失,但是键盘仍然存在并从数字键盘切换到标准文本键盘,并且是关闭的唯一方法这是通过单击设备上的后退按钮。这不是用户友好的,也不是我想要的,因为当用户切换到另一个选项卡时键盘应该关闭。

我创建了下面的代码,如果它当前在屏幕上打开,它应该关闭键盘。

private OnTabChangeListener MyOnTabChangeListener = new OnTabChangeListener(){
        @Override

            public void onTabChanged(String tabId) {
            Log.d("TAG", "I am Inside the Tab Changed Function!!");
                if(getTabHost().getCurrentTabTag().equals("SecondTab")) {
                    Log.d("TAG", "I am Inside the SecondTab Section!!");

                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
                    if(imm != null)
                    { 
                        Log.d("TAG", "I am Inside the SecondTab imm null!!");
                        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 
                    }

                }
                if(getTabHost().getCurrentTabTag().equals("FirstTab")) {
                    Log.d("TAG", "I am Inside the FirstTab Section!!");


                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
                    if(imm != null)
                    { 
                        Log.d("TAG", "I am Inside the FirstTab imm null!!");
                        imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT); 
                    } 

                }
            }


    };

上面的代码确实像我转到第二个选项卡并且键盘在第一个选项卡上打开时一样工作,但是如果我接着说第三个选项卡然后回到第二个选项卡它会重新 -打开我不想要的键盘,因为我只需要第一个选项卡上的键盘。

谁能帮我解决这个问题,因为我不敢相信仅仅控制软键盘就这么难。

提前致谢

编辑....

我什至尝试在 SecondTab 点击监听器中使用下面的代码,但没有成功:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
                    if(imm != null)
                    { 
                        Log.d("TAG", "I am Inside the SecondTab imm null!!");
                        imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); 
                    }

编辑 2.....

我很欣赏下面的第一个响应,但是我尝试将其添加到活动本身,但由于某种原因它不起作用:SI 也尝试了许多不同的东西,但没有运气我什至将以下内容添加到我的清单文件中,但是这不起作用,令人费解:s

android:windowSoftInputMode="stateHidden"

这应该有效,但没有,这真的很烦人。我的清单文件的一部分如下:

 <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".TabActivityTop"
            android:label="@string/app_name" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="Tab1" android:screenOrientation="portrait"></activity>
        <activity android:name="Tab2" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden"></activity>
        <activity android:name="Tab3" android:screenOrientation="portrait"></activity>
        <activity android:name="Tab4" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden"></activity>
    </application>

如果有帮助,我还将完整的 TabActivityTop.java 文件包含在下面:

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class TabActivityTop extends TabActivity {

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

        TabHost tabHost = getTabHost();

        TabSpec tab1spec = tabHost.newTabSpec("Tab1");
        tab1spec.setIndicator("", getResources().getDrawable(R.drawable.icon_tab1_tab));
        Intent tab1Intent = new Intent(this, tab1.class);
        tab1spec.setContent(tab1Intent);

        TabSpec tab2spec = tabHost.newTabSpec("");
        tab2spec.setIndicator("", getResources().getDrawable(R.drawable.icon_tab2_tab));
        Intent tab2Intent = new Intent(this, tab2.class);
        tab2spec.setContent(tab2Intent);

        TabSpec tab3spec = tabHost.newTabSpec("");
        tab3spec.setIndicator("", getResources().getDrawable(R.drawable.icon_tab3_tab));
        Intent tab3Intent = new Intent(this, tab3.class);
        tab3spec.setContent(tab3Intent);

        TabSpec tab4spec = tabHost.newTabSpec("Tab4");
        tab4spec.setIndicator("", getResources().getDrawable(R.drawable.icon_tab4_tab));
        Intent tab4Intent = new Intent(this, tab4.class);
        tab4spec.setContent(tab4Intent);

        tabHost.setOnTabChangedListener(MyOnTabChangeListener);

        // Adding all TabSpec to TabHost
        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);
        tabHost.addTab(tab4);
    }

    private OnTabChangeListener MyOnTabChangeListener = new OnTabChangeListener(){
        @Override

            public void onTabChanged(String tabId) {
            Log.d("TAG", "I am Inside the Tab Changed Function!!");
                if(getTabHost().getCurrentTabTag().equals("contact")) {
                    Log.d("TAG", "I am Inside the Tab4 Section!!");

                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
                    if(imm != null)
                    { 
                        Log.d("TAG", "I am Inside the imm null!!");
                        imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); 
                    }

                }
                if(getTabHost().getCurrentTabTag().equals("Tab1")) {
                    Log.d("TAG", "I am Inside the Tab1 Section!!");
                }
            }

    };


}

我真的希望有人能帮我解决这个问题:)

【问题讨论】:

  • 有没有人有任何想法或者可以告诉我我哪里出错了?
  • 嗨 Mertuarez,我已经阅读了附加的链接,但我无法在活动中播放该代码,因为它没有 EditText 元素。问题仍然是按下选项卡时键盘没有关闭。由于某种原因它仍然存在,有什么想法吗??
  • 从布局中移除
  • 我在任何视图中都没有

标签: android android-tabhost android-softkeyboard


【解决方案1】:

试试这样:

 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 inputMethodManager.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

或者尝试使用:

 rootLayout.requestFocus();

【讨论】:

  • 这会去哪里?在活动上还是在选项卡更改侦听器中?
  • 你想要的地方,我想在 onTabChanged()
猜你喜欢
  • 2022-12-06
  • 1970-01-01
  • 2011-02-04
  • 2016-06-25
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多