一、设计要求:
①UI界面:
1.有“登录”标题显示
2.供用户输入的两个用户名密码输入框
3.供用户选择的两种登陆方式(用户名登录方式、邮箱地址登陆方式)
4.供可勾选的记住登录状态
5.登录按钮
6.登录信息提示
②功能:
1.当用户登录用户名或密码与内设数据(用户名:zhangsan 密码123456 or 邮箱:zhangsan@sample.com 密码 123456)不一致时文本框提示
2.成功登录时文本框提示
3.当用户未选择登录方式时消息提示
二、设计框架:
布局界面:
线性布局 垂直分布 从上至下为 TextView、EditText、EditText、RadioGroup、CheckBox、Button、TextView.
Java代码:
1.按钮监听方式:通过按钮的setOnClickListener()方法注册监听事件,在监听事件中创建OnClickListener(),自动重写onClick()方法。
2.登陆提示实现:
用户名登陆方式:
if (edUN.getText().toString().equals(DbUser)) {
if (edpwd.getText().toString().equals(Dbpassword)) {
message.setText("登录成功");
} else {
message.setText("密码有误");
}
} else {
message.setText("用户名不存在");
}
邮箱地址登陆方式:
if (edUN.getText().toString().equals(DbUser)) {
if (edpwd.getText().toString().equals(Dbpassword)) {
message.setText("登录成功");
} else {
message.setText("密码有误");
}
} else {
message.setText("该邮箱地址不存在");
}
3.未选择登录方式时消息提示:
Toast.makeText(MainActivity.this, "您未选择登录方式" + "\n" + "请选择用户名登录或邮箱地址登录", Toast.LENGTH_SHORT).show();
三、全部代码:
布局界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="登录"
android:textSize="30sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="用户名"
android:inputType="none|text" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="密码"
android:inputType="none|text" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbtLByUN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="用户名登录" />
<RadioButton
android:id="@+id/rbtLByE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="邮箱地址登录" />
</RadioGroup>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记住登录状态" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="登录" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button);
final RadioButton rbtnLByUN = (RadioButton) findViewById(R.id.rbtLByUN);
final RadioButton rbtnLByE = (RadioButton) findViewById(R.id.rbtLByE);
final EditText edUN = (EditText) findViewById(R.id.editText1);
final EditText edpwd = (EditText) findViewById(R.id.editText2);
final TextView message = (TextView) findViewById(R.id.message);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String DbUser, Dbpassword;
if (rbtnLByUN.isChecked()) {
DbUser = "zhangsan";
Dbpassword = "123456";
if (edUN.getText().toString().equals(DbUser)) {
if (edpwd.getText().toString().equals(Dbpassword)) {
message.setText("登录成功");
} else {
message.setText("密码有误");
}
} else {
message.setText("用户名不存在");
}
} else if (rbtnLByE.isChecked()) {
DbUser = "zhangsan@sample.com";
Dbpassword = "123456";
if (edUN.getText().toString().equals(DbUser)) {
if (edpwd.getText().toString().equals(Dbpassword)) {
message.setText("登录成功");
} else {
message.setText("密码有误");
}
} else {
message.setText("该邮箱地址不存在");
}
} else {
Toast.makeText(MainActivity.this, "您未选择登录方式" + "\n" + "请选择用户名登录或邮箱地址登录", Toast.LENGTH_SHORT).show();
}
}
});
}
}
四、模拟器模拟结果: