自定义AlertDialog
public class BaseAlertDialog implements OnClickListener{
private AlertDialog mAlertDialog;
private EditText number_et;
private Button cancel_btn;
private Button confirm_btn;
private Context mContext;
public BaseAlertDialog(Context context)
{
this.mContext = context;
mAlertDialog = new AlertDialog.Builder(mContext).create();
mAlertDialog.show();
mAlertDialog.setContentView(R.layout.base_dialog_main);
number_et = (EditText) mAlertDialog.findViewById(R.id.number_et);
cancel_btn = (Button) mAlertDialog.findViewById(R.id.cancel_btn);
confirm_btn = (Button) mAlertDialog.findViewById(R.id.confirm_btn);
cancel_btn.setOnClickListener(this);
confirm_btn.setOnClickListener(this);
number_et.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
((InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
mAlertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
mAlertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
});
}
private DialogNumber mDialogNumber;
public DialogNumber getmDialogNumber() {
return mDialogNumber;
}
public void setmDialogNumber(DialogNumber mDialogNumber) {
this.mDialogNumber = mDialogNumber;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cancel_btn:
mAlertDialog.dismiss();
break;
case R.id.confirm_btn:
if (number_et.getText().length() != 0) {
mDialogNumber.callBack(number_et.getText().toString());
mAlertDialog.dismiss();
}else{
Toast.makeText(mContext, " 值不能为空", 2000).show();
}
break;
}
}
public interface DialogNumber{
void callBack(String string);
}
}
主MainActivity:
public class MainActivity extends FragmentActivity implements OnClickListener,DialogNumber{
private Button btn_dialog;
private TextView number_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_dialog = (Button) findViewById(R.id.btn_dialog);
number_tv = (TextView) findViewById(R.id.number_tv);
btn_dialog.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_dialog:
new BaseAlertDialog(MainActivity.this).setmDialogNumber(MainActivity.this);
break;
default:
break;
}
}
@Override
public void callBack(String string) {
number_tv.setText(string);
}
}
如果觉得《Android AlertDiaog自定义EditText自动弹出软键盘》对你有帮助,请点赞、收藏,并留下你的观点哦!