我有一个带有ViewPager设置的Tabs的Activity,在其中一个片段中有一个EditText,它在Activity启动时打开软输入键盘,这不是我想要的。我一直试图隐藏它但是当我试图获取一个WindowToken它总是为null - 在EditText本身,在Fragment的根视图上,在父活动的ContentView上(我试过{{1}在带有findViewById(android.R.id.content)
的ContentView上获取的每个视图上,有和没有getDecorView()
,有和没有getRootView()
)。 getChildAt()
也返回null。
此外,我尝试从getCurrentFocus()
和我保留在Application类中的引用中获取父Activity,它们的工作方式相同。
我的最终目标是隐藏键盘,或者根本不显示键盘,除非用户明确地调用它。如果我必须找到一种方法来获得一个WindowToken,我想这样做,但如果有另一种隐藏键盘的方法,我也很乐意尝试。
我想知道为什么这个问题被低估了。如果我不知道原因,我无法改善它。
相关代码的比特:
activity_main.xml中:
getActivity()
app_bar_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/windowBackground"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
layout="@layout/drawer" />
</android.support.v4.widget.DrawerLayout>
(在任何人说什么之前,Scroll不是我的想法)
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.company.app.activities.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:theme="@style/AppTheme.AppBarOverlay">
...
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/underline_grey"
app:tabMode="scrollable"
app:tabGravity="center"
app:popupTheme="@style/AppTheme.PopupOverlayBg"/>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:scrollbars="vertical"
android:orientation="vertical"
app:layout_anchor="@+id/appbar_layout"
android:fillViewport="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
layout="@layout/bg"
android:layout_width="match_parent"
android:layout_height="800dp" />
<include
android:id="@+id/pager_frame"
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/></FrameLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
fragment_get_bonuses.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.company.app.activities.MainActivity"
tools:showIn="@layout/app_bar_main">
<android.support.v4.view.ViewPager
android:id="@+id/main_page_pager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical"
android:gravity="center"
tools:context="com.company.app.fragments.GetBonusesFragment">
...
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/guideline50"
app:layout_constraintBottom_toTopOf="@+id/guideline70"
card_view:contentPadding="4dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="10dp"
card_view:cardBackgroundColor="@color/windowBackground"
android:layout_span="6">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="4dp"
android:gravity="center"
android:weightSum="1">
<EditText
android:id="@+id/bonuses_code_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight=".5"
android:padding="8dp"
android:layout_marginRight="4dp"
android:background="@drawable/border_grey"
android:hint="xxxx-xxxx-xxxx"
android:maxLines="1"
android:lines="1"
android:inputType="text"
android:nextFocusForward="@+id/bonuses_code_card"/>
<android.support.v7.widget.CardView
android:id="@+id/bonuses_code_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
card_view:contentPadding="0dp"
card_view:cardUseCompatPadding="false"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="10dp"
card_view:cardBackgroundColor="@color/pinkAcivateCode"
android:layout_weight=".5"
android:clickable="true"
android:onClick="onMenuButtonsClick">
<TextView
android:id="@+id/bonuses_code_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/activate_code"
android:textColor="@color/textColorPrimary"
android:lines="2"
android:padding="8dp" />
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v7.widget.CardView>
...
<android.support.constraint.Guideline
android:id="@+id/guideline50"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline70"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp" />
...
</android.support.constraint.ConstraintLayout>
}
申请类:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
AppCompatActivity context;
...
TabLayout mainPageTabs;
View pagerView;
ViewPager mainPagePager;
MainPageTabsAdapter adapter;
TextView tabBonuses;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fresco.initialize(this);
setContentView(R.layout.activity_main);
context = this;
App.getContext().setCurrent(context); // saves reference to currently running Activity in Application class
...
pagerView = findViewById(R.id.pager_frame);
mainPageTabs = (TabLayout) findViewById(R.id.tabs);
...
setupMainTabs();
}
public void setupMainTabs(){
if (mainPagePager == null) mainPagePager = (ViewPager) findViewById(R.id.main_page_pager);
if (mainPageTabs == null) mainPageTabs = (TabLayout) findViewById(R.id.tabs);
setupMainPageViewPager(mainPagePager);
mainPageTabs.setupWithViewPager(mainPagePager,true);
...
tabBonuses = (TextView) LayoutInflater.from(this).inflate(R.layout.tab_item, null); // TextView with a background
tabBonuses.setText(getText(R.string.collect_bonuses));
...
mainPageTabs.getTabAt(0).setCustomView(tabBonuses);
...
// InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.hideSoftInputFromWindow(findViewById(R.id.pager_frame).getWindowToken(), 0);
}
private void setupMainPageViewPager(ViewPager viewPager) {
adapter = new MainPageTabsAdapter(getSupportFragmentManager(),this);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(2);
}
...
@Override
protected void onResume() {
super.onResume();
App.getContext().setCurrent(context);
...
// InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.hideSoftInputFromWindow(findViewById(R.id.pager_frame).getWindowToken(), 0);
}
...
MainPageTabsAdapter.java:
...
private static App context;
private AppCompatActivity current;
...
@Override
public void onCreate() {
super.onCreate();
if (context == null) context = (DreamClub) getApplicationContext();
...
}
...
public void setCurrent(AppCompatActivity a) {
current = a;
}
public Activity getCurrent(){return current;}
...
public static App getContext() {
if (context == null) context = new DreamClub();
return context;
}
GetBonusesFragment.java:
public class MainPageTabsAdapter extends FragmentPagerAdapter {
Context context;
ArrayList<Fragment> fragmList;
ArrayList<String> titleList;
public MainPageTabsAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
fragmList = new ArrayList<>();
fragmList.add(new GetBonusesFragment());
...
titleList = new ArrayList<>();
titleList.add(context.getString(R.string.collect_bonuses));
...
}
@Override
public Fragment getItem(int position) {
return fragmList.get(position);
}
...
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
答案 0 :(得分:1)
只需在清单活动
中添加此内容即可机器人:windowSoftInputMode =&#34; stateHidden&#34;
<activity
android:name=".activity.SignupActivity"
android:windowSoftInputMode="stateHidden" />
答案 1 :(得分:0)
在清单文件中添加
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
或尝试
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
但是如果您的要求像按钮上的隐藏keyborad点击或backPressed那么就像这样使用
public static void HideKeyboard(Context context, View view) {
if (view == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (!imm.isActive()) {
return;
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
请更多关注此链接