Java tutorial
/* * Copyright (c) 2015, . * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.yamei.shop.chat.widget; import android.app.Activity; import android.content.Context; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RelativeLayout; import com.yamei.shop.R; import com.yamei.shop.chat.listener.OnOperationListener; import com.yamei.shop.chat.listener.SoftKeyboardStateHelper; import com.yamei.shop.chat.adapter.FaceCategroyAdapter; import java.util.List; /** * ? * * @author kymjs (http://www.kymjs.com/) */ public class KJChatKeyboard extends RelativeLayout implements SoftKeyboardStateHelper.SoftKeyboardStateListener { public interface OnToolBoxListener { void onShowFace(); } public static final int LAYOUT_TYPE_HIDE = 0; public static final int LAYOUT_TYPE_FACE = 1; public static final int LAYOUT_TYPE_MORE = 2; /** * */ private EditText mEtMsg; private CheckBox mBtnFace; private CheckBox mBtnMore; private Button mBtnSend; /** * */ private ViewPager mPagerFaceCagetory; private RelativeLayout mRlFace; private PagerSlidingTabStrip mFaceTabs; private int layoutType = LAYOUT_TYPE_HIDE; private FaceCategroyAdapter adapter; //? private List<String> mFaceData; private Context context; private OnOperationListener listener; private OnToolBoxListener mFaceListener; private SoftKeyboardStateHelper mKeyboardHelper; public KJChatKeyboard(Context context) { super(context); init(context); } public KJChatKeyboard(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public KJChatKeyboard(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } private void init(Context context) { this.context = context; View root = View.inflate(context, R.layout.chat_tool_box, null); this.addView(root); } @Override protected void onFinishInflate() { super.onFinishInflate(); initData(); this.initWidget(); } private void initData() { mKeyboardHelper = new SoftKeyboardStateHelper(((Activity) getContext()).getWindow().getDecorView()); mKeyboardHelper.addSoftKeyboardStateListener(this); } private void initWidget() { mEtMsg = (EditText) findViewById(R.id.toolbox_et_message); mBtnSend = (Button) findViewById(R.id.toolbox_btn_send); mBtnFace = (CheckBox) findViewById(R.id.toolbox_btn_face); mBtnMore = (CheckBox) findViewById(R.id.toolbox_btn_more); mRlFace = (RelativeLayout) findViewById(R.id.toolbox_layout_face); mPagerFaceCagetory = (ViewPager) findViewById(R.id.toolbox_pagers_face); mFaceTabs = (PagerSlidingTabStrip) findViewById(R.id.toolbox_tabs); adapter = new FaceCategroyAdapter(((FragmentActivity) getContext()).getSupportFragmentManager(), LAYOUT_TYPE_FACE); mBtnSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (listener != null) { String content = mEtMsg.getText().toString(); listener.send(content); mEtMsg.setText(null); } } }); // mBtnFace.setOnClickListener(getFunctionBtnListener(LAYOUT_TYPE_FACE)); // ?? mBtnMore.setOnClickListener(getFunctionBtnListener(LAYOUT_TYPE_MORE)); // ? mEtMsg.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { hideLayout(); } }); } /************************* * start ************************/ private OnClickListener getFunctionBtnListener(final int which) { return new OnClickListener() { @Override public void onClick(View v) { if (isShow() && which == layoutType) { hideLayout(); showKeyboard(context); } else { changeLayout(which); showLayout(); mBtnFace.setChecked(layoutType == LAYOUT_TYPE_FACE); mBtnMore.setChecked(layoutType == LAYOUT_TYPE_MORE); } } }; } private void changeLayout(int mode) { adapter = new FaceCategroyAdapter(((FragmentActivity) getContext()).getSupportFragmentManager(), mode); adapter.setOnOperationListener(listener); layoutType = mode; setFaceData(mFaceData); } @Override public void onSoftKeyboardOpened(int keyboardHeightInPx) { hideLayout(); } @Override public void onSoftKeyboardClosed() { } /***************************** end ******************************/ /************************** * ? start **************************/ public void setFaceData(List<String> faceData) { mFaceData = faceData; adapter.refresh(faceData); mPagerFaceCagetory.setAdapter(adapter); mFaceTabs.setViewPager(mPagerFaceCagetory); if (layoutType == LAYOUT_TYPE_MORE) { mFaceTabs.setVisibility(GONE); } else { //1emoji?? if (faceData.size() + 1 < 2) { mFaceTabs.setVisibility(GONE); } else { mFaceTabs.setVisibility(VISIBLE); } } } public EditText getEditTextBox() { return mEtMsg; } public void showLayout() { hideKeyboard(this.context); // ????? postDelayed(new Runnable() { @Override public void run() { mRlFace.setVisibility(View.VISIBLE); if (mFaceListener != null) { mFaceListener.onShowFace(); } } }, 50); } public boolean isShow() { return mRlFace.getVisibility() == VISIBLE; } public void hideLayout() { mRlFace.setVisibility(View.GONE); if (mBtnFace.isChecked()) { mBtnFace.setChecked(false); } if (mBtnMore.isChecked()) { mBtnMore.setChecked(false); } } /** * ?? */ public void hideKeyboard(Context context) { Activity activity = (Activity) context; if (activity != null) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isActive() && activity.getCurrentFocus() != null) { imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); } } } /** * */ public static void showKeyboard(Context context) { Activity activity = (Activity) context; if (activity != null) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } } public OnOperationListener getOnOperationListener() { return listener; } public void setOnOperationListener(OnOperationListener onOperationListener) { this.listener = onOperationListener; adapter.setOnOperationListener(onOperationListener); } public void setOnToolBoxListener(OnToolBoxListener mFaceListener) { this.mFaceListener = mFaceListener; } /*********************** ? end ******************************/ }