android 登录界面编写
1、登录页面实现内容
1.实现使用两个EditText输入框输入用户名和密码。
2.使用CheckBox控件记住密码功能。
3.登录时候,验证用户名和密码是否为空。
4.当前CheckBox控件记住密码勾上时,使用SharedPreferences存储用户名和密码。
5.登录时候使用ProgressDialog登录转圈圈2秒,两秒后显示登录成功。
6.默认用户名和密码admin和admin。当用户名和密码输入都是admin就提示登录成功。
2、登录页面布局实现
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvTitle"
style="@style/TextView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="登录"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/layoutInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@id/tvTitle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
style="@style/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/layout_left_distance"
android:text="用户名:" />
<EditText
android:id="@+id/editUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/layout_right_diatance"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
style="@style/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/layout_left_distance"
android:text="密 码:" />
<EditText
android:id="@+id/editPsw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/layout_right_diatance"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/layoutCheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/layoutInput"
android:gravity="center">
<CheckBox
android:id="@+id/checkBoxRemember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="记住密码" />
</LinearLayout>
<Button
android:id="@+id/buttonLogin"
style="@style/button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:text="登录"
app:layout_constraintTop_toBottomOf="@id/layoutCheck"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
3、fragment实现登录界面
public class LoginFragment extends Fragment {
private ProgressDialog progressDialog = null;
private View rootView;
private EditText editUser, editPsw;
private CheckBox checkBoxRemember;
public LoginFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_loglin, container, false);
editUser = rootView.findViewById(R.id.editUser);
editPsw = rootView.findViewById(R.id.editPsw);
checkBoxRemember = rootView.findViewById(R.id.checkBoxRemember);
SharedPreferences sp = getActivity().getSharedPreferences("mmsx", MODE_PRIVATE);
editUser.setText(sp.getString("user",""));
editPsw.setText(sp.getString("password",""));
checkBoxRemember.setChecked(sp.getBoolean("remember",true));
View buttonLogin = rootView.findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(view -> {
String user = editUser.getText().toString();
String psw = editPsw.getText().toString();
if (user.isEmpty()){
Toast.makeText(getActivity(),"请输入用户名", Toast.LENGTH_LONG).show();
return;
}
if (psw.isEmpty()){
Toast.makeText(getActivity(),"请输入密码", Toast.LENGTH_LONG).show();
return;
}
if (user.equalsIgnoreCase("admin") && psw.equalsIgnoreCase("admin")){
SharedPreferences.Editor edit = Objects.requireNonNull(getActivity()).getSharedPreferences("mmsx", MODE_PRIVATE).edit();
if (checkBoxRemember.isChecked()){
edit.putString("user", "admin");
edit.putString("password", "admin");
}else {
edit.putString("user", "");
edit.putString("password", "");
}
edit.putBoolean("remember", checkBoxRemember.isChecked());
edit.apply();
progressDialog=new ProgressDialog(getActivity());
progressDialog.setTitle("登录中");
progressDialog.setMessage("登录中,请稍后...");
progressDialog.setCancelable(true);
progressDialog.show();
//这里的话新建一个线程,重写run()方法,
new Thread()
{
public void run()
{
SystemClock.sleep(2000);//把信息码发送给handle让更新界面
handler.sendEmptyMessage(123);
}
}.start();
}else {
Toast.makeText(getActivity(),"用户名或者密码错误", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
@SuppressLint("HandlerLeak")
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 123) {
progressDialog.dismiss();
Toast.makeText(getActivity(),"登录成功", Toast.LENGTH_LONG).show();
}
}
};
}
4、实现成果
原文地址:https://blog.csdn.net/qq_16064871/article/details/144621775
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!