private void initBottomView() { // 最左边 btn_chat_add = (Button) findViewById(R.id.btn_chat_add); btn_chat_emo = (Button) findViewById(R.id.btn_chat_emo); btn_chat_add.setOnClickListener(this); btn_chat_emo.setOnClickListener(this); // 最右边 btn_chat_keyboard = (Button) findViewById(R.id.btn_chat_keyboard); btn_chat_voice = (Button) findViewById(R.id.btn_chat_voice); btn_chat_voice.setOnClickListener(this); btn_chat_keyboard.setOnClickListener(this); btn_chat_send = (Button) findViewById(R.id.btn_chat_send); btn_chat_send.setOnClickListener(this); // 最下面 layout_more = (LinearLayout) findViewById(R.id.layout_more); layout_emo = (LinearLayout) findViewById(R.id.layout_emo); layout_add = (LinearLayout) findViewById(R.id.layout_add); initAddView(); initEmoView(); // 最中间 // 语音框 btn_speak = (Button) findViewById(R.id.btn_speak); // 输入框 edit_user_comment = (EmoticonsEditText) findViewById(R.id.edit_user_comment); edit_user_comment.setOnClickListener(this); edit_user_comment.addTextChangedListener( new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (!TextUtils.isEmpty(s)) { btn_chat_send.setVisibility(View.VISIBLE); btn_chat_keyboard.setVisibility(View.GONE); btn_chat_voice.setVisibility(View.GONE); } else { if (btn_chat_voice.getVisibility() != View.VISIBLE) { btn_chat_voice.setVisibility(View.VISIBLE); btn_chat_send.setVisibility(View.GONE); btn_chat_keyboard.setVisibility(View.GONE); } } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void afterTextChanged(Editable s) {} }); }
/** * 根据是否点击笑脸来显示文本输入框的状态 @Title: showEditState * * @param @param isEmo: 用于区分文字和表情 * @return void * @throws */ private void showEditState(boolean isEmo) { edit_user_comment.setVisibility(View.VISIBLE); btn_chat_keyboard.setVisibility(View.GONE); btn_chat_voice.setVisibility(View.VISIBLE); btn_speak.setVisibility(View.GONE); edit_user_comment.requestFocus(); if (isEmo) { layout_more.setVisibility(View.VISIBLE); layout_more.setVisibility(View.VISIBLE); layout_emo.setVisibility(View.VISIBLE); layout_add.setVisibility(View.GONE); hideSoftInputView(); } else { layout_more.setVisibility(View.GONE); showSoftInputView(); } }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.edit_user_comment: // 点击文本输入框 mListView.setSelection(mListView.getCount() - 1); if (layout_more.getVisibility() == View.VISIBLE) { layout_add.setVisibility(View.GONE); layout_emo.setVisibility(View.GONE); layout_more.setVisibility(View.GONE); } break; case R.id.btn_chat_emo: // 点击笑脸图标 if (layout_more.getVisibility() == View.GONE) { showEditState(true); } else { if (layout_add.getVisibility() == View.VISIBLE) { layout_add.setVisibility(View.GONE); layout_emo.setVisibility(View.VISIBLE); } else { layout_more.setVisibility(View.GONE); } } break; case R.id.btn_chat_add: // 添加按钮-显示图片、拍照、位置 if (layout_more.getVisibility() == View.GONE) { layout_more.setVisibility(View.VISIBLE); layout_add.setVisibility(View.VISIBLE); layout_emo.setVisibility(View.GONE); hideSoftInputView(); } else { if (layout_emo.getVisibility() == View.VISIBLE) { layout_emo.setVisibility(View.GONE); layout_add.setVisibility(View.VISIBLE); } else { layout_more.setVisibility(View.GONE); } } break; case R.id.btn_chat_voice: // 语音按钮 edit_user_comment.setVisibility(View.GONE); layout_more.setVisibility(View.GONE); btn_chat_voice.setVisibility(View.GONE); btn_chat_keyboard.setVisibility(View.VISIBLE); btn_speak.setVisibility(View.VISIBLE); hideSoftInputView(); break; case R.id.btn_chat_keyboard: // 键盘按钮,点击就弹出键盘并隐藏掉声音按钮 showEditState(false); break; case R.id.btn_chat_send: // 发送文本 final String msg = edit_user_comment.getText().toString(); if (msg.equals("")) { ShowToast("请输入发送消息!"); return; } boolean isNetConnected = NetStateUtils.isNetworkAvailable(this); if (!isNetConnected) { ShowToast(R.string.network_error); // return; } // 组装BmobMessage对象 BmobMsg message = BmobMsg.createTextSendMsg(this, targetId, msg); message.setExtra("Bmob"); // 默认发送完成,将数据保存到本地消息表和最近会话表中 manager.sendTextMessage(targetUser, message); // 刷新界面 refreshMessage(message); break; case R.id.tv_camera: // 拍照 selectImageFromCamera(); break; case R.id.tv_picture: // 图片 selectImageFromLocal(); break; case R.id.tv_location: // 位置 selectLocationFromMap(); break; default: break; } }
/** * 刷新界面 @Title: refreshMessage * * @param @param messsage * @return void * @throws */ private void refreshMessage(BmobMsg msg) { // 更新界面 mAdapter.add(msg); mListView.setSelection(mAdapter.getCount() - 1); edit_user_comment.setText(""); }