I am new to android, and I would like to implement a tab layout and a viewpager infragments,showing a pop up window, such that when I click a button the pop up window could cover the keyboard and I could view the tab layout within. If it's possible, then is there any example I could refer to?
EDIT: Let me rephrase, What I wanted to ask was how can I put the tab layout and view pager in the pop up window.
这是处理弹出窗口
创建的PopupViewHandler类public class PopupViewHandler {
AppCompatActivity mActivity;
PopupWindow window;
Context mContext;
LayoutInflater layoutInflater;
View popupView;
PopupViewHandler(AppCompatActivity mActivity, Context mContext) {
this.mActivity = mActivity;
this.mContext = mContext;
}
public void setupstuff() {
layoutInflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.tab_layout, (ViewGroup) mActivity.findViewById(R.id.main_layout));
TabLayout tabLayout = (TabLayout) popupView.findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) popupView.findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(mActivity.getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
window = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
window.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
}
这是tab_layout xml。
<LinearLayout
android:id="@+id/main_layout"
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
这就是我通过主要活动按钮点击启动此课程的方法 -
handler = new PopupViewHandler(MainActivity.this, getBaseContext());
handler.setupstuff();
但是当我运行它并单击按钮时,我收到以下错误:
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: java.lang.IllegalArgumentException: No view found for id 0x7f0c0085 (com.example.himanshu.myemoticons:id/pager) for fragment FragmentTab{3db8038f #0 id=0x7f0c0085}
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.View.measure(View.java:17448)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.View.measure(View.java:17448)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1164)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1402)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:580)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:550)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5312)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
02-26 20:23:17.882 11982-11982/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
答案 0 :(得分:0)
您可以使用 InputMethodManager 隐藏虚拟键盘,调用hideSoftInputFromWindow
,传入包含您视图的窗口的标记。
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
您需要使用上下文来启动InputMethodManager
,然后您可以将此代码用于所需的视图。