1.无法弹出输入键盘
有人说,不让我extends alertdialog
让我改写为dialog;
发现不行;
方法二:
1.解决无法弹出输入法:
在show()方法调用之前,用dialog.setView(new EditText(context))添加一个空的EditText,由于是自定义的AlertDialog,有我们指定的布局,所以设置这个不会影响我们的功能,这样就可以弹出输入法了……
看我的全能代码:
public class SearchContentDialog extends AlertDialog {
private View.OnClickListener mListener;
private Context mContext;
private TextView tvwNetInfo;
private ImageView mImageView;
public Button mBtnLoad;
private String content;
private boolean isShowAgainLoad = true;
private LinearLayout llBtnSearch;
private EditText edtContent;
private SearchContentListener searchContentListener;
private InputMethodManager imm;
public SearchContentDialog(Context context, String content, View.OnClickListener listener) {
super(context);
this.mContext = context;
this.content = content;
this.mListener = listener;
this.setCanceledOnTouchOutside(false);
}
public SearchContentDialog(Context context, SearchContentListener searchContentListener) {
super(context);
this.mContext = context;
this.searchContentListener = searchContentListener;
this.setCanceledOnTouchOutside(false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_search_content);
edtContent = (EditText) findViewById(R.id.edt_search_content);
imm = (InputMethodManager) mContext.getSystemService(INPUT_METHOD_SERVICE);
edtContent.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
imm.showSoftInput(v, 0);
} else {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
});
edtContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
LogUtils.e("sjt", "触发了搜索");
searchContentListener.searchByContent(edtContent.getText().toString());
return true;
}
return false;
}
});
llBtnSearch = (LinearLayout) findViewById(R.id.ll_search_content);
TextView tvwCancle = (TextView) findViewById(R.id.tvw_cancle);
tvwCancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
llBtnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchContentListener.searchByContent(edtContent.getText().toString());
}
});
}
@Override
public void show() {
super.show();
/**
* 设置宽度全屏,要设置在show的后面
*/
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.gravity = Gravity.CENTER;
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
getWindow().getDecorView().setPadding(0, 0, 0, 0);
getWindow().setAttributes(layoutParams);
}
@Override
public void onBackPressed() {
Activity activity = (Activity) mContext;
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
params.alpha = 0.5f;
activity.getWindow().setAttributes(params);
activity.finish();
super.onBackPressed();
}
public void showKeyboard() {
if (edtContent != null) {
//设置可获得焦点
edtContent.setFocusable(true);
edtContent.setFocusableInTouchMode(true);
//请求获得焦点
edtContent.requestFocus();
//调用系统输入法
InputMethodManager inputManager = (InputMethodManager) edtContent
.getContext().getSystemService(INPUT_METHOD_SERVICE);
inputManager.showSoftInput(edtContent, 0);
}
}
public interface SearchContentListener {
public void searchByContent(String content);
}
}
2.透明度问题
1.设置透明度(Dialog自身的透明度)
WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();
lp.alpha=1.0f;
dialog.getWindow().setAttributes(lp);
alpha在0.0f到1.0f之间。1.0完全不透明,0.0f完全透明
2.设置黑暗度(Dialog自身的黑暗度)
dialog.setContentView(R.layout.dialog);
WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();
lp.dimAmount=1.0f;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dimAmount在0.0f和1.0f之间,0.0f完全不暗,1.0f全暗
3.设置Dialog底背景模糊和黑暗度
WindowManager.LayoutParams.FLAG_BLUR_BEHIND(设置模糊)
WindowManager.LayoutParams.FLAG_DIM_BEHIND(设置暗淡)
4.清除Dialog底背景模糊和黑暗度
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND)