代码展示:
MainActivity:
package com.example.lab1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText userName;
private EditText userPass;
private ArrayList<CheckBox> chose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
userName = findViewById(R.id.userName);
userPass = findViewById(R.id.userPass);
chose=new ArrayList<CheckBox>();
CheckBox box1=(CheckBox) findViewById(R.id.checkbox1);
CheckBox box2=(CheckBox) findViewById(R.id.checkbox2);
CheckBox box3=(CheckBox) findViewById(R.id.checkbox3);
chose.add(box1);
chose.add(box2);
chose.add(box3);
}
public void login(View view){
StringBuffer sb = new StringBuffer();
sb.append("登陆成功!"+"\n"+"\n"+"\n");
sb.append("用户名:" + userName.getText().toString()+"\n"+"\n");
sb.append("密码:" + userPass.getText().toString()+"\n"+"\n");
sb.append("登陆选项: "+getCheck()+"\n");
Toast.makeText(this, "正在登录...", Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setClass(this, ResultActivity.class);
intent.putExtra("info", sb.toString());
this.startActivity(intent);
}
public String getCheck()
{
StringBuilder strChoose=new StringBuilder("");
for(CheckBox cb:chose){
if(cb.isChecked()){
strChoose.append(cb.getText().toString()+" ");
}
}
String str=strChoose.toString();
if(!"".equals(str))
str=str.substring(0,strChoose.length()-1);
else
str="默认登陆";
return str;
}
public void forgetPass(View view){
StringBuilder sb = new StringBuilder();
sb.append("忘记密码!!!");
Intent intent = new Intent();
intent.setClass(this, ForgetPassActivity.class);
intent.putExtra("info", sb.toString());
this.startActivity(intent);
}
public void register(View view){
StringBuilder sb = new StringBuilder();
sb.append("注册账号!!!");
Intent intent = new Intent();
intent.setClass(this, RegisterActivity.class);
intent.putExtra("info", sb.toString());
this.startActivity(intent);
}
}
main的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFF4EF"
tools:context=".MainActivity">
<ImageView
android:layout_width="720px"
android:layout_height="281px"
android:layout_gravity="center_horizontal"
android:src="@mipmap/logo"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="10dp"
android:src="@mipmap/ico3" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="20dp" />
<EditText
android:id="@+id/userName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20dp" />
<EditText
android:id="@+id/userPass"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="" />
</TableRow>
</TableLayout>
</LinearLayout>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="登 陆"
android:textSize="20dp"
android:onClick="login"
/>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textSize="20dp"
android:checked="true"
/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登陆"
android:textSize="20dp"
android:checked="true"
/>
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接受产品推广"
android:textSize="20dp"
android:checked="true"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="忘记密码"
android:textSize="20dp"
android:onClick="forgetPass"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="80dp"
android:text="注册账号"
android:textSize="20dp"
android:onClick="register"
/>
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
>
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
style="?android:attr/progressBarStyleSmall"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="loading..."
android:textSize="20dp" />
</RelativeLayout>
</LinearLayout>
ResultActivity:
package com.example.lab1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView result = (TextView) findViewById(R.id.result);
result.setText("\n"+"MainActivity传送内容如下:\n" +this.getIntent().getStringExtra("info"));
}
}
result的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResultActivity">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
/>
</LinearLayout>
RegisterActivity:
package com.example.lab1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
public void register(View v) {
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
String s = "完成注册,请再次登录";
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
startActivity(intent);
}
}
register的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegisterActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册账号"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="24dp"
android:layout_marginLeft="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="24dp"
android:layout_marginLeft="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"
android:textSize="24dp"
android:layout_marginLeft="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请再次输入密码"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成注册"
android:textSize="20dp"
android:layout_gravity="center_horizontal"
android:onClick="register"
/>
</LinearLayout>
ForgetPassActivity:
package com.example.lab1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ForgetPassActivity extends AppCompatActivity {
private EditText edt_NewPassWord, edt_NewPassWord2;
private Button btn_Confirm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forget_pass);
edt_NewPassWord = this.findViewById(R.id.edt_NewPassWord);
edt_NewPassWord2 = this.findViewById(R.id.edt_NewPassWord2);
btn_Confirm = this.findViewById(R.id.btn_Confirm);
edt_NewPassWord.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
} else {
String newPassword = edt_NewPassWord.getText().toString();
}
}
});
edt_NewPassWord2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
} else {
String newPassword = edt_NewPassWord.getText().toString();
String newPassword2 = edt_NewPassWord2.getText().toString();
if (!(newPassword.equals(newPassword2))) {
Toast.makeText(ForgetPassActivity.this, "两次密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
edt_NewPassWord2.setText("");
}
}
}
});
btn_Confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String password = edt_NewPassWord.getText().toString();
String password2 = edt_NewPassWord2.getText().toString();
if (password.equals("") || password2.equals("")) {
Toast.makeText(ForgetPassActivity.this, "请输入完整信息", Toast.LENGTH_SHORT).show();
} else if (!(password.equals(password2))) {
Toast.makeText(ForgetPassActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ForgetPassActivity.this, "修改密码成功,请登录!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ForgetPassActivity.this, MainActivity.class);
startActivity(intent);
}
}
});
}
}
forget布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ForgetPassActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改密码"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="132dp"
android:layout_height="wrap_content"
android:text="新 密 码:"
android:textSize="24dp"
android:layout_marginLeft="10dp"/>
<EditText
android:id="@+id/edt_NewPassWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请 输 入 新 密 码"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="132dp"
android:layout_height="wrap_content"
android:text="确认新密码:"
android:textSize="24dp"
android:layout_marginLeft="10dp"/>
<EditText
android:id="@+id/edt_NewPassWord2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请再次输入新密码"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<Button
android:id="@+id/btn_Confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认修改"
android:textSize="20dp"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>