当前位置: 代码迷 >> Android >> 自定义Dialog+OnKeyListener,该怎么解决
  详细解决方案

自定义Dialog+OnKeyListener,该怎么解决

热度:28   发布时间:2016-05-01 12:08:18.0
自定义Dialog+OnKeyListener
在FirstActivity中,点击button后,弹出一个输入密码对话框,在CustomDialog 这边实现按键输入,并判断输入的密码是否正确!
问题:怎样让CustomDialog这边无论输入密码正确与否,都要返回一个值给FirstActivity这边,因为FirstActivity这样要根据返回值来判断是否继续执行下面的函数。
CustomDialog.java

public class CustomDialog extends Dialog implements Constants{

private Context mContext;
private int iDoWhat;
private LayoutInflater mInflater;
private View passwordView;
private Dialog passwordDialog;
private int passwordValue;
private StringBuffer strBufferPassword;
private boolean bReturn = false;

private ImageView passNormalImage1;
private ImageView passNormalImage2;
private ImageView passNormalImage3;
private ImageView passNormalImage4;

private dbaseManager dbase;
private BOX_INFO_STRUCT stBoxInfo;

public CustomDialog(Context context)
{
super(context);
this.mContext = context;
this.mInflater = LayoutInflater.from(mContext);

strBufferPassword = new StringBuffer();

setView();
initDialog();
}

private void setView()
{
passwordView = mInflater.inflate(R.layout.input_password_layout, null);
passNormalImage1 = (ImageView) passwordView.findViewById(R.id.pass_normal1_id);
passNormalImage2 = (ImageView) passwordView.findViewById(R.id.pass_normal2_id);
passNormalImage3 = (ImageView) passwordView.findViewById(R.id.pass_normal3_id);
passNormalImage4 = (ImageView) passwordView.findViewById(R.id.pass_normal4_id);
}

private void initDialog()
{
passwordDialog = new Dialog(mContext, R.style.Theme_password);

Window dialogWindow = passwordDialog.getWindow();
dialogWindow.setGravity(Gravity.CENTER_VERTICAL);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// params.width = LayoutParams.MATCH_PARENT;
// params.height = LayoutParams.MATCH_PARENT;
passwordDialog.setCancelable(true);

passwordDialog.setCanceledOnTouchOutside(false);
passwordDialog.addContentView(passwordView, params);
}

public void show()
{
passwordDialog.show();
passwordDialog.setOnKeyListener(inputPassword);
}

private OnKeyListener inputPassword = new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) 
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode) {
case KEY_0:
// System.out.println("It's 0");
SetImageAlpha(0);
break;
case KEY_1:
// System.out.println("It's 1");
SetImageAlpha(1);
break;
case KEY_2:
// System.out.println("It's 2");
SetImageAlpha(2);
break;
case KEY_3:
// System.out.println("It's 3");
SetImageAlpha(3);
break;
case KEY_4:
// System.out.println("It's 4");
SetImageAlpha(4);
break;
case KEY_5:
// System.out.println("It's 5");
SetImageAlpha(5);
break;
case KEY_6:
// System.out.println("It's 6");
SetImageAlpha(6);
 break;
case KEY_7:
  相关解决方案