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.lm.im_huanxin.ui.widget; import android.app.Activity; import android.content.Context; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.text.Editable; import android.text.TextWatcher; 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.lm.im_huanxin.R; import com.lm.im_huanxin.adapter.FaceCategroyAdapter; import com.lm.im_huanxin.ui.fragment.OnOperationListener; import com.lm.im_huanxin.ui.fragment.SoftKeyboardStateHelper; import java.util.ArrayList; 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 = new ArrayList<String>(); 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); mEtMsg.addTextChangedListener(mTextWatcher); mEtMsg.setSelection(mEtMsg.length()); 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(); } }); } //?? private TextWatcher mTextWatcher = new TextWatcher() { private int editStart; private int editEnd; public void afterTextChanged(Editable s) { editStart = mEtMsg.getSelectionStart(); editEnd = mEtMsg.getSelectionEnd(); // ?? mEtMsg.removeTextChangedListener(mTextWatcher); // ???EditText?? // ??calculateLength1 while (calculateLength(s.toString()) > 2000) { // ??? s.delete(editStart - 1, editEnd); editStart--; editEnd--; } mEtMsg.setSelection(editStart); // ??? mEtMsg.addTextChangedListener(mTextWatcher); setLeftCount(); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } }; /** * =?= ??????1 * * @param c * @return */ private long calculateLength(CharSequence c) { double len = 0; for (int i = 0; i < c.length(); i++) { int tmp = (int) c.charAt(i); if (tmp > 0 && tmp < 127) { len += 0.5; } else { len++; } } return Math.round(len); } /** * */ private void setLeftCount() { if (getInputCount() == 0) { mBtnSend.setVisibility(INVISIBLE); mBtnMore.setVisibility(VISIBLE); } else { mBtnSend.setVisibility(VISIBLE); mBtnMore.setVisibility(INVISIBLE); } // mFontSize.setText(getInputCount()+"/"+MAX_FONT_SIZE); } /** * ? * * @return */ private long getInputCount() { return calculateLength(mEtMsg.getText().toString()); } /************************* * 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 ******************************/ }