赞
踩
该实验是紧接上一期使用SP登录、记住密码的实验,主要实现是除了上次登陆以外,登录以后在主界面有一系列假数据,可以通过右上角进行排序选择,以下是实验效果:
示例:好友名排序效果
首先是登陆界面以及逻辑代码,在逻辑代码中实现记住密码(勾选选择框后)
activity_login3.xml
- <?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"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#E6E6E6"
- android:orientation="vertical"
- android:padding="10dp">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="15dp"
- android:background="@android:color/white"
- android:orientation="horizontal">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="10dp"
- android:text="账号:"
- android:textColor="#000"
- android:textSize="20sp" />
- <EditText
-
- android:id="@+id/et_account"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- android:background="@null"
- android:padding="10dp" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:background="@android:color/white"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/tv_password"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="10dp"
- android:text="密码:"
- android:textColor="#000"
- android:textSize="20sp" />
-
- <EditText
- android:id="@+id/et_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- android:background="@null"
- android:inputType="textPassword"
- android:padding="10dp" />
- </LinearLayout>
-
- <CheckBox
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="记住密码"
- android:id="@+id/cb"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btn_login"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="25dp"
- android:background="#3C8DC4"
- android:text="登录"
- android:textColor="@android:color/white"
- android:textSize="20sp" />
- <Button
- android:id="@+id/btn_reg"
- android:layout_weight="1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="25dp"
- android:background="#3C8DC4"
- android:text="注册"
- android:textColor="@android:color/white"
- android:textSize="20sp" />
- </LinearLayout>
-
- </LinearLayout>
登陆成功后跳转到主界面,主界面由一个ListView和右上角抽屉Menu构成
以下是主界面的界面代码
activity_main60.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity60">
-
- <ListView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/lv"/>
- </LinearLayout>
因为用到了ListView,所以需要一个item的布局代码:
frin_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:padding="10dp">
-
- <ImageView
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:src="@drawable/txlogo" />
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:id="@+id/id"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="ID:1"
- android:textSize="20dp" />
-
- <TextView
- android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="姓名:李"
- android:textSize="20dp" />
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView
- android:id="@+id/sex"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="性别:女"
- android:textSize="20dp" />
-
- <TextView
- android:id="@+id/phone"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="电话:123123"
- android:textSize="20dp" />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
menu的代码:(在res下创建menu文件夹在文件夹中创建menu.xml)
menu.xml
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/menu_order"
- android:icon="@android:drawable/ic_menu_help"
- android:title="排序"
- />
- </menu>
接下来是主界面的逻辑代码,需要处理几件事:设置好友初始化数据、设置ListView适配器以及显示数据、初始化Menu、点击Menu能够跳转到选择排序的设置界面
值得注意的是适配器采用内部类的方式写在了MainActivity60中
以下是逻辑代码:
首先是登录界面的逻辑代码(上期内容)
SharedHelper
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.widget.Toast;
-
- import java.util.HashMap;
- import java.util.Map;
-
- public class SharedHelper {
-
- private Context mContext;
-
- public SharedHelper() {
- }
-
- public SharedHelper(Context mContext) {
- this.mContext = mContext;
- }
-
- //定义一个保存数据的方法
- public void save(String username, String passwd) {
- SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- editor.putString("username", username);
- editor.putString("passwd", passwd);
- editor.commit();
-
- }
-
- //定义一个读取SP文件的方法
- public Map<String, String> read() {
- Map<String, String> data = new HashMap<String, String>();
- SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
- data.put("username", sp.getString("username", ""));
- data.put("passwd", sp.getString("passwd", ""));
- return data;
- }
- //清空SP
- public void clear(){
- SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- editor.clear();
- editor.commit();
- }
- }
LoginActivity
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.Toast;
-
- import java.util.Map;
-
- public class LoginActivity extends AppCompatActivity {
- private EditText et_account, et_password;
- private Button btn_login,reg_login;
- private CheckBox cb;
- private SharedHelper sh;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login3);
- et_account = (EditText)findViewById(R.id.et_account);
- et_password = (EditText)findViewById(R.id.et_password);
- cb = (CheckBox)findViewById(R.id.cb);
- //判断SharedPreferences中是否保存name的值
-
- sh = new SharedHelper(LoginActivity.this);
- Map<String, String> data = sh.read();
- et_account.setText(data.get("username"));
- et_password.setText(data.get("passwd"));
- if(!et_account.getText().toString().isEmpty()){
- cb.setChecked(true);
- }
-
- btn_login = findViewById(R.id.btn_login);
- btn_login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String username = et_account.getText().toString();
- String passwd = et_password.getText().toString();
- if (username.isEmpty() || passwd.isEmpty()) {
- Toast.makeText(LoginActivity.this, "请输入完整账号密码", Toast.LENGTH_SHORT).show();
- } else {
- if (cb.isChecked()) {
- sh.save(username, passwd);
- } else {
- sh.clear();
- }
- Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
- Intent intent=new Intent(LoginActivity.this,MainActivity60.class);
- startActivity(intent);
- }
-
-
- }
- });
- }
- }
MainActivity60
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
-
- public class MainActivity60 extends AppCompatActivity {
- List<Friend> friends=new ArrayList<>();
- ListView lv;
- myDapter myAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main60);
- if(friends.size()<1){
- friends.add(new Friend("1","张三","123123","男"));
- friends.add(new Friend("2","李四","321","男"));
- friends.add(new Friend("3","王五","43534","男"));
- friends.add(new Friend("4","喜羊羊","4364","男"));
- friends.add(new Friend("5","美羊羊","74568","女"));
- friends.add(new Friend("6","懒羊羊","76864","男"));
- friends.add(new Friend("7","沸羊羊","3234","男"));
- friends.add(new Friend("8","慢羊羊","5436","男"));
- }
- myAdapter=new myDapter();
- lv=findViewById(R.id.lv);
- lv.setAdapter(myAdapter);
-
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
- //排序
- SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
- int sortBy = pref.getInt("sort", 1);
- //创建比较器
- Comparator<Friend> comparator = null;
- switch(sortBy){
- case 1:
- comparator = new SortById();
- break;
- case 2:
- comparator = new SortByName();
- break;
- case 3:
- comparator = new SortByPhone();
- break;
- case 4:
- comparator = new SortBySex();
- break;
- }
-
- Collections.sort(friends, comparator);
- myAdapter.notifyDataSetChanged();
- super.onActivityResult(requestCode, resultCode, data);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu,menu);
- return super.onCreateOptionsMenu(menu);
- }
-
- @Override
- public boolean onOptionsItemSelected(@NonNull MenuItem item) {
- switch (item.getItemId()){
- case R.id.menu_order:
- Intent intent1 = new Intent(MainActivity60.this,SettingActivity.class);
- startActivityForResult(intent1, 0);
- break;
- }
- return super.onOptionsItemSelected(item);
- }
-
- class myDapter extends BaseAdapter{
-
- @Override
- public int getCount() {
- return friends.size();
- }
-
- @Override
- public Object getItem(int position) {
- return friends.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view=View.inflate(MainActivity60.this,R.layout.frin_item,null);
- TextView id=view.findViewById(R.id.id);
- TextView name=view.findViewById(R.id.name);
- TextView sex=view.findViewById(R.id.sex);
- TextView phone=view.findViewById(R.id.phone);
- id.setText("ID:"+friends.get(position).getId());
- name.setText("姓名:"+friends.get(position).getName());
- sex.setText("性别:"+friends.get(position).getSex());
- phone.setText("电话:"+friends.get(position).getPhone());
- return view;
- }
- }
- }
还差一个用于选择排序的Activity,界面由RadioGroup以及RadioButton构成,在逻辑代码中则是在SP中设置选择的排序并返回主界面。
activity_setting.xml
- <?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="wrap_content"
- android:orientation="vertical"
- tools:context=".SettingActivity">
-
- <RadioGroup
- android:id="@+id/rgSortMethod"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="排序方式" />
-
- <RadioButton
- android:id="@+id/rdoSortById"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="按ID排序" />
-
- <RadioButton
- android:id="@+id/rdoSortByName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="按好友名字排序" />
-
- <RadioButton
- android:id="@+id/rdoSortByPhone"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="按电话号码排序" />
-
- <RadioButton
- android:id="@+id/rdoSortBySex"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="按性别排序" />
- </RadioGroup>
-
- <Button
- android:id="@+id/btn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="确定" />
- </LinearLayout>
SettingActivity
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.view.View;
- import android.widget.RadioGroup;
-
- public class SettingActivity extends AppCompatActivity {
- RadioGroup rgSortBy;
- SharedPreferences pref;
- int sortBy;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_setting);
- rgSortBy = (RadioGroup) findViewById(R.id.rgSortMethod);
- pref = PreferenceManager.getDefaultSharedPreferences(this);
- sortBy = pref.getInt("sort", 1);
- switch (sortBy) {
- case 1:
- rgSortBy.check(R.id.rdoSortById);
- break;
- case 2:
- rgSortBy.check(R.id.rdoSortByName);
- break;
- case 3:
- rgSortBy.check(R.id.rdoSortByPhone);
- break;
- case 4:
- rgSortBy.check(R.id.rdoSortBySex);
- break;
-
- }
- rgSortBy.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- switch (checkedId) {
- case R.id.rdoSortById:
- sortBy = 1;
- break;
- case R.id.rdoSortByName:
- sortBy = 2;
- break;
- case R.id.rdoSortByPhone:
- sortBy = 3;
- break;
- case R.id.rdoSortBySex:
- sortBy = 4;
- break;
-
- }
- }
- });
- findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //保存排序方式
- pref.edit().putInt("sort", sortBy).commit();
- Intent intent = new Intent();
- Bundle bundle = new Bundle();
- bundle.putString("type", "order");
- intent.putExtras(bundle);
- intent.setClass(SettingActivity.this, MainActivity.class);
- setResult(RESULT_OK, intent);
- finish();
- }
- });
- }
- }
另外,前面用到一个实体类,这里给出
Friend
- public class Friend {
- private String id;
- private String name;
- private String phone;
- private String sex;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getPhone() {
- return phone;
- }
-
- public void setPhone(String phone) {
- this.phone = phone;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public Friend(String id, String name, String phone, String sex) {
- this.id = id;
- this.name = name;
- this.phone = phone;
- this.sex = sex;
- }
- }
还有几个比较器:(结构都大致相同的)
SortById
- import java.text.Collator;
- import java.util.Comparator;
- import java.util.Locale;
-
- public class SortByName implements Comparator<Friend> {
- @Override
- public int compare(Friend f1, Friend f2) {
- Comparator comparator = Collator.getInstance(Locale.CHINESE); //这里是中文,如果不是中文,//里面的Locale.CHINESE可以去掉
- return comparator.compare(f1.getId(), f2.getId());
- }
- }
SortByName
- import java.text.Collator;
- import java.util.Comparator;
- import java.util.Locale;
-
- public class SortByName implements Comparator<Friend> {
- @Override
- public int compare(Friend f1, Friend f2) {
- Comparator comparator = Collator.getInstance(Locale.CHINESE); //这里是中文,如果不是中文,//里面的Locale.CHINESE可以去掉
- return comparator.compare(f1.getName(), f2.getName());
- }
- }
SortByPhone
- import java.text.Collator;
- import java.util.Comparator;
- import java.util.Locale;
-
- public class SortByPhone implements Comparator<Friend> {
- @Override
- public int compare(Friend f1, Friend f2) {
- Comparator comparator = Collator.getInstance(Locale.CHINESE); //这里是中文,如果不是中文,//里面的Locale.CHINESE可以去掉
- return comparator.compare(f1.getPhone(), f2.getPhone());
- }
- }
SortBySex
- import java.text.Collator;
- import java.util.Comparator;
- import java.util.Locale;
-
- public class SortBySex implements Comparator<Friend> {
- @Override
- public int compare(Friend f1, Friend f2) {
- Comparator comparator = Collator.getInstance(Locale.CHINESE); //这里是中文,如果不是中文,//里面的Locale.CHINESE可以去掉
- return comparator.compare(f1.getSex(), f2.getSex());
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。