//声明相应的程序包
package com.example.sample4_1;
//导入程序所依赖的类库
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.view.*;
import android.widget.*;
//创建MainActivity继承于Activity类
public class MainActivity extends Activity {
//一般应从其它途径获取用户名和密码,这里定义成常量
String strUserName="TestUser";
String strPWD="123456";
EditText txt_UserName;
EditText txt_PWD;
Button btn_Login;
Button btn_Cancel;
//重写onCreate()方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_UserName=(EditText)this.findViewById(R.id.txt_UserName);
txt_PWD=(EditText)this.findViewById(R.id.txt_PWD);
btn_Login=(Button)this.findViewById(R.id.btn_Login);
btn_Cancel=(Button)this.findViewById(R.id.btn_Cancel);
btn_Login.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
String str_UserName=txt_UserName.getText().toString();
String str_PWD=txt_PWD.getText().toString();
if(str_UserName.equals(strUserName) && str_PWD.equals(strPWD)){
Toast toast=Toast.makeText(MainActivity.this, "身份验证通过!", Toast.LENGTH_SHORT);
toast.show();
//可接着转向其它页面处理,这里等待系统自动结束程序
Intent intent=new Intent(MainActivity.this,NextDealActivity.class);
startActivity(intent);
}
else{
Toast toast=Toast.makeText(MainActivity.this, "身份错误!", Toast.LENGTH_SHORT);
toast.show();
txt_UserName.setText("");
txt_PWD.setText("");
}
}
});
btn_Cancel.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//强制结束Activity
finish();
}
});
}
}
零零赫
相关分类