alertDialog创建登陆界面,判断用户输入

alertDialog创建登陆界面,需要获取用户输入的用户名和密码,获取控件对象的时候不能像主布局文件那样获得,

需要在onClickListener中获取,代码如下:

 public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case 1:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
break;
case 2:
Toast.makeText(MainActivity.this, "POWER BY BOF", Toast.LENGTH_LONG).show();
break;
case 3:
login();
break;
}
return super.onOptionsItemSelected(item);
} //用户注册
public void login(){
/*
* 此段代码为重点内容
*
*/
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
final View v1 = factory.inflate(R.layout.login, null);
//创建对话框
final Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("用户登陆");
alertDialog.setView(v1);
alertDialog.setPositiveButton("登录", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
/*
* 下面两行代码为获取dialog中的控件对象,
* 和UI布局获取控件对象略有不同,只能在用到alertDialog的地方进行实现
*/
EditText user = (EditText)v1.findViewById(R.id.user);
EditText passwd = (EditText)v1.findViewById(R.id.passwd);
String userStr = user.getText().toString();
String passwdStr = passwd.getText().toString();
if(userStr.equals("android") && passwdStr.equals("asdf123..")){
Intent intent1 = new Intent(MainActivity.this,HandproActivity.class);
startActivity(intent1);
}else{
Toast.makeText(MainActivity.this, "账号或密码错误,请重新输入", Toast.LENGTH_LONG).show();
} //Toast.makeText(MainActivity.this, "haha", Toast.LENGTH_LONG).show();
}
});
alertDialog.setNegativeButton("去注册", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
alertDialog.create().show();
}
上一篇:centos 7 update to python V3.43 to assure git clone as usual


下一篇:android中提示&对话框----AlertDialog