跟随键盘变化的布局
来源:互联网 发布:淘宝网男士休闲运动鞋 编辑:程序博客网 时间:2024/05/12 17:46
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@color/list_item_background_normal" android:gravity="top|left" android:hint="@string/tweet_hint" android:padding="@dimen/space_12" android:textColor="@color/main_black" android:textSize="@dimen/text_size_16" /> <RelativeLayout android:id="@+id/bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/list_item_background_normal" android:gravity="bottom" android:padding="@dimen/space_8" ><!-- 显示一张图片右上角带有删除按钮的布局 --> <RelativeLayout android:id="@+id/rl_img" android:layout_width="60dp" android:layout_height="60dp" android:visibility="gone" > <ImageView android:id="@+id/iv_img" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:background="@color/light_gray" android:clickable="true" android:scaleType="centerCrop" /> <ImageView android:id="@+id/iv_clear_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@drawable/ib_clear_image_selector" /> </RelativeLayout> <TextView android:id="@+id/tv_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/rl_img" android:layout_alignParentRight="true" android:background="@drawable/compose_clear_selector" android:clickable="true" android:gravity="center_vertical" android:textColor="@color/main_gray" android:textSize="@dimen/text_size_14" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/list_divider_color" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/list_item_background_normal" android:orientation="horizontal" android:weightSum="4" > <ImageButton android:id="@+id/ib_picture" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:padding="@dimen/space_8" android:src="@drawable/compose_toolbar_picture_selector" /> <ImageButton android:id="@+id/ib_mention" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:padding="@dimen/space_8" android:src="@drawable/compose_toolbar_mention_selector" /> <ImageButton android:id="@+id/ib_trend_software" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:padding="@dimen/space_8" android:src="@drawable/compose_toolbar_trend_selector" /> <ImageButton android:id="@+id/ib_emoji_keyboard" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:padding="@dimen/space_8" android:src="@drawable/compose_toolbar_emoji_selector" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/list_divider_color" /> <FrameLayout android:id="@+id/emoji_keyboard_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" /></LinearLayout>private final EmojiKeyboardFragment keyboardFragment = new EmojiKeyboardFragment(); // 用表情键盘填充帧布局容器 getFragmentManager().beginTransaction() .replace(R.id.emoji_keyboard_fragment, keyboardFragment).commit(); keyboardFragment.setOnEmojiClickListener(new OnEmojiClickListener() { @Override public void onEmojiClick(Emojicon v) { InputHelper.input2OSC(mEtInput, v); } @Override public void onDeleteButtonClick(View v) { InputHelper.backspace(mEtInput); } }); case R.id.ib_emoji_keyboard: if (!keyboardFragment.isShow()) { // emoji隐藏中 keyboardFragment.showEmojiKeyBoard(); keyboardFragment.hideSoftKeyboard(); } else { keyboardFragment.hideEmojiKeyBoard(); keyboardFragment.showSoftKeyboard(mEtInput); } break;/** * 表情键盘 */public class EmojiKeyboardFragment extends Fragment implements SoftKeyboardStateListener { private LinearLayout mEmojiContent; private RadioGroup mEmojiBottom; /** 底部工具条按钮集合,不包含删除按钮 */ private View[] mEmojiTabs; private ViewPager mEmojiPager; private EmojiPagerAdapter adapter; /** 表情键盘根布局 包含显示表情的ViewPger和底部水平工具条 */ private LinearLayout mRootView; private OnEmojiClickListener listener; public static int EMOJI_TAB_CONTENT; private SoftKeyboardStateHelper mKeyboardHelper; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); mRootView = (LinearLayout) inflater.inflate(R.layout.frag_keyboard, container, false); initWidget(mRootView); return mRootView; } private void initWidget(View rootView) { // bottom 底部工具条布局 mEmojiBottom = (RadioGroup) rootView.findViewById(R.id.emoji_bottom); mEmojiBottom.setVisibility(View.VISIBLE); EMOJI_TAB_CONTENT = mEmojiBottom.getChildCount() - 1; // 减一是因为有一个删除按钮 mEmojiTabs = new View[EMOJI_TAB_CONTENT]; if (EMOJI_TAB_CONTENT <= 1) { // 只有一个分类的时候就不显示了 mEmojiBottom.setVisibility(View.GONE); } for (int i = 0; i < EMOJI_TAB_CONTENT; i++) { mEmojiTabs[i] = mEmojiBottom.getChildAt(i); /** 给工具条按钮添加单击监听器,根据序号来切换对应的viewPager页 */ mEmojiTabs[i].setOnClickListener(getBottomBarClickListener(i)); } mEmojiBottom.findViewById(R.id.emoji_bottom_del).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { if (listener != null) { listener.onDeleteButtonClick(v); } } }); // content必须放在bottom下面初始化 mEmojiContent = (LinearLayout) rootView.findViewById(R.id.emoji_content); mEmojiPager = (ViewPager) mEmojiContent.findViewById(R.id.emoji_pager); adapter = new EmojiPagerAdapter(getFragmentManager(), EMOJI_TAB_CONTENT, listener); mEmojiPager.setAdapter(adapter); mEmojiContent.setVisibility(View.VISIBLE); mKeyboardHelper = new SoftKeyboardStateHelper(getActivity().getWindow().getDecorView()); mKeyboardHelper.addSoftKeyboardStateListener(this); } /** * 底部栏点击事件监听器 * @param indexfff * @return */ private OnClickListener getBottomBarClickListener(final int index) { return new OnClickListener() { @Override public void onClick(View v) { mEmojiPager.setCurrentItem(index); } } }; public void setOnEmojiClickListener(OnEmojiClickListener l) { this.listener = l; } public void hideAllKeyBoard() { hideEmojiKeyBoard(); hideSoftKeyboard(); } public boolean isShow() { return mEmojiContent.getVisibility() == View.VISIBLE; } /** * 隐藏Emoji并显示软键盘 */ public void hideEmojiKeyBoard() { mEmojiBottom.setVisibility(View.GONE); mEmojiContent.setVisibility(View.GONE); } /** * 显示Emoji并隐藏软键盘 */ public void showEmojiKeyBoard() { mEmojiContent.setVisibility(View.VISIBLE); if (EMOJI_TAB_CONTENT > 1) { mEmojiBottom.setVisibility(View.VISIBLE); } } /** * 隐藏软键盘 */ public void hideSoftKeyboard() { ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mEmojiBottom.getWindowToken(), 0); } /** * 显示软键盘 */ public void showSoftKeyboard(EditText et) { ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(et, InputMethodManager.SHOW_FORCED); } /** * 当软键盘显示时回调 */ @Override public void onSoftKeyboardOpened(int keyboardHeightInPx) { mEmojiBottom.setVisibility(View.GONE); mEmojiContent.setVisibility(View.GONE); } @Override public void onSoftKeyboardClosed() {} @Override public void onStop() { super.onStop(); hideSoftKeyboard(); }}/** * 表情页适配器(FragmentPagerAdapter的好处是fragment常驻内存,对于要求效率而页卡很少的表情控件最合适) */public class EmojiPagerAdapter extends FragmentPagerAdapter { private OnEmojiClickListener listener; public EmojiPagerAdapter(FragmentManager fm, int tabCount, OnEmojiClickListener l) { super(fm); KJEmojiFragment.EMOJI_TAB_CONTENT = tabCount; listener = l; } public EmojiPagerAdapter(FragmentManager fm) { super(fm); } @Override public EmojiPageFragment getItem(int index) { if (KJEmojiFragment.EMOJI_TAB_CONTENT > 1) { return new EmojiPageFragment(index, index, listener); } else { return new EmojiPageFragment(index, 0, listener); } /** * 显示模式:如果只有一种Emoji表情,则像QQ表情一样左右滑动分页显示<br> * 如果有多种Emoji表情,每页显示一种,Emoji筛选时上下滑动筛选。 */@Override public int getCount() { if (KJEmojiFragment.EMOJI_TAB_CONTENT > 1) { return KJEmojiFragment.EMOJI_TAB_CONTENT; } else { // 采用进一法取小数 return (DisplayRules.getAllByType(0).size() + KJEmojiConfig.COUNT_IN_PAGE -1) / KJEmojiConfig.COUNT_IN_PAGE; } } public interface OnEmojiClickListener { void onDeleteButtonClick(View v); void onEmojiClick(Emojicon v); }}/** * 键盘状态监听辅助类 */public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener { /** * 键盘打开关闭回调接口 */ public interface SoftKeyboardStateListener { void onSoftKeyboardOpened(int keyboardHeightInPx); void onSoftKeyboardClosed(); } /** 观察者集合 */ private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>(); private final View activityRootView; /** 键盘高度 */ private int lastSoftKeyboardHeightInPx; /** 键盘是否打开标识位 */ private boolean isSoftKeyboardOpened; public SoftKeyboardStateHelper(View activityRootView) { this(activityRootView, false); } public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) { this.activityRootView = activityRootView; this.isSoftKeyboardOpened = isSoftKeyboardOpened; activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { final Rect r = new Rect(); // r will be populated with the coordinates of your view that area still visible. // activityRootView.getWindowVisibleDisplayFrame(r); // 获取窗体高度变化差 final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top); if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard... isSoftKeyboardOpened = true; notifyOnSoftKeyboardOpened(heightDiff); } else if (isSoftKeyboardOpened && heightDiff < 100) { isSoftKeyboardOpened = false; notifyOnSoftKeyboardClosed(); } } public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) { this.isSoftKeyboardOpened = isSoftKeyboardOpened; } public boolean isSoftKeyboardOpened() { return isSoftKeyboardOpened; } /** * Default value is zero (0) * @return last saved keyboard height in px */ public int getLastSoftKeyboardHeightInPx() { return lastSoftKeyboardHeightInPx; } public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.add(listener); } public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) { listeners.remove(listener); } private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) { this.lastSoftKeyboardHeightInPx = keyboardHeightInPx; for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardOpened(keyboardHeightInPx); } } } private void notifyOnSoftKeyboardClosed() { for (SoftKeyboardStateListener listener : listeners) { if (listener != null) { listener.onSoftKeyboardClosed(); } } }}
1 0
- 跟随键盘变化的布局
- DataGridView跟随窗体的变化还变化
- 用CSS解决左右两块布局时,分界线跟随二者中高度大的变化
- 键盘的通知 文本框跟随键盘移动
- 监测键盘的变化而变化-NSNotification
- 获取键盘的高度变化
- PlayMaker GUI跟随布局的使用
- BCB6的窗体界面能够跟随xp的主题变化
- 绘图(二,跟随路径变化的Text)
- 实现跟随手指移动的小球—跟随移动颜色变化哦!
- 监听屏幕布局的变化
- 布局变化时的动画
- 布局变化时的动画
- UITextField跟随键盘移动
- UITextfield 跟随 键盘
- unity3d 摄像机跟随鼠标和键盘的控制
- 上边图片跟随点击的图片发生变化,点击图片样式发生变化
- 适应ios中键盘高度的变化
- 【HDU】-1176-免费馅饼(DP,好)
- 整洁代码-----有意义的命名
- 分布式队列编程模型
- Android 读取<meta-data>元素的数据
- 51Nod-1163-最高的奖励
- 跟随键盘变化的布局
- HTML <form> 标签的 enctype 属性
- 杭电oj--1018 Big Number
- jsp文件中格式化时间
- UFLDL stack2params.m
- 关于tomcat中全局参数context-param中的一点了解
- Shark, Spark SQL, Hive on Spark, 以及SQL on Apache Spark的未来
- __VA_ARGS__
- 自定义Dialog,view根据需要自定义填充