谷歌建议使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其声明周期,它和Fragment有着基本一致的声明周期。且DialogFragment也允许开发者把AlertDialog作为内嵌的组件进行重用,类似Fragment(DialogFragment可以在大屏幕和小屏幕显示出不同的效果,屏幕旋转相关数据不会被销毁)
普通自定义1:----------------------------------------------------------------------------------
public class MyDialog1 extends DialogFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//无标题栏
View view = inflater.inflate(R.layout.dialogfragment, container,false);
return view;
}
}
主类:
extends FragmentActivity implements OnClickListener
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_title:
MyDialog1 dialog = new MyDialog1();dialog.show(getSupportFragmentManager(), "tag");break; } }
推荐自定义2:------------------------------------------------------------------------------------
public class MyDialog extends DialogFragment implements OnClickListener{
private EditText et_title;
//自定义接口监听
public interface OnInputListener {
void onInputComplete(String title);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View views=LayoutInflater.from(getActivity()).inflate(R.layout.dialogfragment, null);
ImageView iv_go = (ImageView) views.findViewById(R.id.ImageView01);
et_title = (EditText) views.findViewById(R.id.et_title);
iv_go.setOnClickListener(this);
AlertDialog.Builder builder =new AlertDialog.Builder(getActivity());
AlertDialog show = builder.show();
show.getWindow().setContentView(views);//自定义布局
show.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);//宽高
show.getWindow().setGravity(Gravity.TOP);//位置 setLayout
show.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//支持输入法show.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return show;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ImageView01:
OnInputListener listener = (OnInputListener) getActivity();
listener.onInputComplete(et_title.getText().toString());//传递值给监听接口
break;
}
}
}
主类:
extends FragmentActivity implements OnClickListener,OnInputListener
@Override
public void onInputComplete(String title) {
Toast.makeText(this, "title:" +title, Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_title:
MyDialog dialog = new MyDialog();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
dialog.show(ft, "tag");
break;
}
}
使用DialogFragment替代PopupWindow可以解决编辑框不能复制粘贴的bug。。。。。。。。。。。。。。。。。。。
附:输入法各值的含义:
InputMethodManager im=(InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(view.getWindowToken(),0);//隐藏输入法
stateUnspecified:软键盘的状态不指定,系统将选择一个合适的状态或依赖于主题的设置
stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示
stateHidden:用户选择activity时,软键盘是被隐藏
stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的
stateVisible:软键盘通常是可见的
stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态
adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示
adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间
adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分
当输入法打乱布局时可以尝试在对应的layout XML的顶级元素上加一层ScrollViewPopupMenu使用 http://blog.csdn.net/loucyin/article/details/50813439