之前在项目中遇到个问题
就是写一个简单的评论框,当时是用popupwindow写的,里面嵌套Edittext,但刚做完就碰到个问题,就是虽然Edittext获得焦点但是不弹出软键盘(有点蛋疼),无奈只好手动去调用和取消键盘
private void autoFocus() {
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.RESULT_SHOWN);
}
protected void autoHide() {
InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
然后本以为这个bug就告一段落了,但事与愿违。第二天老板说自己要评论,需要粘贴一些文字,但是粘贴不了。在android中,在Edittext中长按一般都会弹出标签来操作复制和粘贴全选等选项,但在我当前的情况并没有。后来就各种找,终于在http://stackoverflow.com/questions/15857755/android-textview-does-not-support-text-selection-action-mode-cancelled,里面说这其实是android的一个bug,所以想要解决是比较困难的。当然也有另一种思路,就是换一种实现方式,用DialogFragment,google也更加推荐用这种方式来写对话框,最后我也采取了这种方案,上面所提到bug没有再出现。
没有用过DialogFragment可以先到下面网址学习
http://blog.csdn.net/lmj623565791/article/details/37815413/
不过要实现上图的样式,稍微有点改动,而且在手指点击阴影部分的时候对话框消失。直接上代码
comment_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/share_content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<ScrollView
android:id="@+id/sl_comm_bot"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/ll_comm_bot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff" >
<EditText
android:id="@+id/comm_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="10dp"
android:layout_weight="4"
android:background="@drawable/shape_common"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="left|top"
android:hint="立即评论..."
android:inputType="textMultiLine"
android:lines="4"
android:maxLength="100" />
<Button
android:id="@+id/comm_bt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@drawable/fabiao_selector"
android:text="发表"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
CommentDialog.java
public class CommentDialog extends DialogFragment{
private View mLayout;
private LayoutInflater mInflater;
private Context mContext;
private Button comm_bt;
private MyEditText comm_et;
private NewsEntity news;
public interface DismissListener
{
void onDismissComplete(boolean isComm);
}
public CommentDialog (NewsEntity news){
this.news = news;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_DialogWhenLarge_NoActionBar);
}
private void autoFocus() {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.RESULT_SHOWN);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mLayout = inflater.inflate(R.layout.comment_dialog, container);
comm_bt = (Button) mLayout.findViewById(R.id.comm_bt);
comm_et = (MyEditText) mLayout.findViewById(R.id.comm_et);
getDialog().getWindow().setBackgroundDrawable(
new ColorDrawable(R.color.alert_dark));
comm_et.requestFocus(); WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
autoFocus();
comm_bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(StringUtil.isEmpty(comm_et.getText().toString().trim())){
Toast.makeText(mContext, "请输入评论内容", 0).show();
return ;
}
comment();
}
});
//点击阴影消失
mLayout.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int top = mLayout.findViewById(R.id.sl_comm_bot).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < top) {
dismiss();
}
}
return true;
}
});
comm_et.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dismiss();
return true;
}
return false;
}
});
return mLayout;
}
//替换回车制表符
public String replaceBlank(String str) {
String dest = "";
if (str!=null) {
Pattern p = Pattern.compile("t|r|n");
Matcher m = p.matcher(str);
dest = m.replaceAll(" ");
}
return dest;
}
private void comment() {
//操作网络请求
}
}
调用
FragmentManager fragmentManager = getFragmentManager();
CommentDialog dialog = new CommentDialog(news);
dialog.show(fragmentManager, "commentDialog");