fragment_home.xml
布局文件代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6">
<ImageView
android:id="@+id/iv"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="140dp"
app:srcCompat="@drawable/m1" />
<LinearLayout
android:id="@+id/uer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:background="#ffffff"
android:orientation="horizontal">
<TextView
android:id="@+id/usernume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="账号:"
android:textColor="#000"
android:textSize="25sp" />
<EditText
android:id="@+id/un"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp"
android:hint="请输入账号"/>
</LinearLayout>
<LinearLayout
android:id="@+id/qqpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/uer"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"
android:orientation="horizontal">
<TextView
android:id="@+id/qq_PW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="密码:"
android:textColor="#000"
android:textSize="25sp" />
<EditText
android:id="@+id/pw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/qq_PW"
android:background="@null"
android:inputType="numberPassword"
android:padding="10dp"
android:hint="请输入密码"/>
</LinearLayout>
<CheckBox
android:id="@+id/rem_pw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/qqpassword"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="记住密码"
android:textColor="#000000" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rem_pw"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#3C8DC4 "
android:text="登录"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/text_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/login"
android:layout_alignStart="@+id/login"
android:layout_alignLeft="@+id/login"
android:layout_marginTop="25dp"
android:text=""
android:textColor="#000"
android:textSize="20sp" />
<LinearLayout
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#E6E6E6"
android:orientation="horizontal">
<Button
android:id="@+id/regest"
android:layout_width="80dp"
android:layout_height="30dp"
android:background="#E6E6E6"
android:text="注册"
android:textColor="#0F7BCF"
android:textSize="16dp" />
<Button
android:id="@+id/forget"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginLeft="220dp"
android:background="#E6E6E6"
android:text="忘记密码"
android:textColor="#0F7BCF"
android:textSize="16dp" />
</LinearLayout>
</RelativeLayout>
效果展示:
因为用的是fragment,所以要在对应的fragment类的onActivityCreated()方法里设置监听器,在activity设置监听器会闪退,与此同时用getActivity()方法获取fragment所在的活动。
HomeFragment
对应java代码:
package com.example.myapplication.ui.home;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
import com.example.myapplication.regest;
import com.example.myapplication.select;
public class HomeFragment extends Fragment {
EditText user_name;
EditText pass_word;
Button login;
Button forget;
Button regest;
CheckBox rempw;
public View onCreateView( LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
return root;
}
public void onActivityCreated(Bundle savedInstanceState){ //在此设置监听器
super.onActivityCreated(savedInstanceState);
user_name = (EditText) getActivity().findViewById(R.id.un);
pass_word = (EditText) getActivity().findViewById(R.id.pw);
login = (Button) getActivity().findViewById(R.id.login);
regest = (Button) getActivity().findViewById(R.id.regest);
forget = (Button) getActivity().findViewById(R.id.forget);
rempw=(CheckBox) getActivity().findViewById(R.id.rem_pw);
SharedPreferences sp=getActivity().getSharedPreferences("date", Context.MODE_PRIVATE);
if(sp.getBoolean("rem",false)==true) { //判断是否保存了密码
rempw.setChecked(true);
SharedPreferences sp1=getActivity().getSharedPreferences("rem", Context.MODE_PRIVATE); //获得保存密码的rem.xml文件
user_name.setText(sp1.getString("username",null));
pass_word.setText(sp1.getString("pwd",null));
}
login.setOnClickListener(new View.OnClickListener() {//登录按钮的监听器绑定
public void onClick(View view) {
// TODO Auto-generated method stub
// new login().login1(user, pass);
SharedPreferences sp=getActivity().getSharedPreferences("date", Context.MODE_PRIVATE); //获取设置的账号和密码
String us = sp.getString("username", null);
String pw = sp.getString("pwd", null);
Boolean rem=sp.getBoolean("rem",false);
String user = user_name.getText().toString();
String pass = pass_word.getText().toString();
if (us.equals(user) && pw.equals(pass)) {
Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
if(rempw.isChecked()){
SharedPreferences sp1 = getActivity().getSharedPreferences("rem", Context.MODE_PRIVATE);
SharedPreferences.Editor edit1 = sp1.edit();
edit1.putString("username", user);
edit1.putString("pwd", pass);
edit1.commit();
}
Intent intent = new Intent(getActivity(), select.class);
startActivity(intent);
} else
Toast.makeText(getActivity(), "账号或密码错误", Toast.LENGTH_SHORT).show();
}
});
rempw.setOnClickListener(new View.OnClickListener() {//保存密码按钮的监听器绑定
public void onClick(View view) {
String user = user_name.getText().toString();
String pass = pass_word.getText().toString();
// TODO Auto-generated method stub
// new login().login1(user, pass);
SharedPreferences sp1 = getActivity().getSharedPreferences("rem", Context.MODE_PRIVATE);
SharedPreferences sp2 = getActivity().getSharedPreferences("date", Context.MODE_PRIVATE);
SharedPreferences.Editor edit1 = sp1.edit();
SharedPreferences.Editor edit2 = sp2.edit();
if(rempw.isChecked()) {
edit1.putString("username", user);
edit1.putString("pwd", pass);
edit2.putBoolean("rem", true);
Toast.makeText(getActivity(), "账号密码已保存", Toast.LENGTH_SHORT).show();
}
else
edit2.putBoolean("rem", false);
edit1.commit();
edit2.commit();
}
});
regest.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent=new Intent(getActivity(), com.example.myapplication.regest.class);
startActivity(intent);
}
});
}
}
效果展示: