1.综合使用RecycleView,CardView,Adapter实现一个宝宝相册,并将其加入到实验一形成的应用中,使得:用户成功登录后转到宝宝相册所在的主界面。还要求实现:用户单击对应的列表子项的不同部位时给出不同的Toast提示或者对话框提示:
- 按钮:“添加成功”对话框
- 其它:“相册详情”
2.修改(登录与注册)的登陆页面,为该页面增加一个复选框,用户可以通过点击的方式来进行选中和取消,选中时表示需要记住密码,下次登陆时应不需要再次输入密码;取消选中时表示不需要记住密码,之前保存的用户名和密码需要被清除,下次登陆时需要重新输入新的密码。getSharedPreferences()存储方法.
MainActivity.java
package com.example.myapplication1;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button button1,button2;
private EditText edit_name_login,edit_pwd_login;
private EditText usernameET,passwordET;
private CheckBox check_box;
private SharedPreferences sp;
private String password_register,username_register;
//@Override
/*protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==222 && resultCode==111){
username_register = data.getStringExtra("username");
password_register = data.getStringExtra("password");
edit_name_login.setText(username_register);
edit_pwd_login.setText(password_register);
}
}*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);//映射activity_main.xml中的id=button1按钮
button2=(Button)findViewById(R.id.button2);//映射activity_main.xml中的id=button2按钮
edit_name_login = (EditText)findViewById(R.id.edit_name_login) ;
edit_pwd_login = (EditText)findViewById(R.id.edit_pwd_login);
check_box = (CheckBox)findViewById(R.id.check_box);
//usernameET =(EditText)findViewById(R.id.edit_name);
//passwordET = (EditText)findViewById(R.id.edit_pwd);
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
boolean flag = sp.getBoolean("flag",false);
check_box.setChecked(flag);
if(flag){
String newName = sp.getString("username","");
String newPwd = sp.getString("password","");
edit_name_login.setText(newName);
edit_pwd_login.setText(newPwd);
}
//final String user = "Admin";
//final String pass = "I love Android" ;
//为两个按钮增加点击事件监听
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = edit_name_login.getText().toString();
String password = edit_pwd_login.getText().toString();
//String password_register1 = passwordET.getText().toString().trim();
//String username_register1= usernameET.getText().toString().trim();
if(TextUtils.isEmpty(username) || TextUtils.isEmpty((password))){
//if((username.equals(username_register))&&(password.equals(password_register))){
String msg = "Wrong username or password.Please try again!";
Toast toast = Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
else
{
SharedPreferences.Editor editor = sp.edit();
if(check_box.isChecked()){
editor.putBoolean("flag",true);
editor.putString("username",username);
editor.putString("password",password);
}
else{
// editor.putBoolean("flag",false).commit();
editor.clear();
}
editor.apply();
Intent intent = new Intent(MainActivity.this,RecycleViewActivity.class);
startActivity(intent);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,SubActivity2.class);
// startActivityForResult(intent,222);
startActivity(intent);
}
});
}
}
SplashActivity.java
package com.example.myapplication1;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
//加上这句设置为全屏不加只隐藏title
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(mainIntent);
//动画效果
overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
finish();
}
},3000);
}
}
SubActivity1.java
package com.example.myapplication1;
import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
public class SubActivity1 extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub1);
/* Button button1;
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = "欢迎进入DIY!";
Toast toast = Toast.makeText(SubActivity1.this,msg,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
});*/
}
}
SubActivity2.java
package com.example.myapplication1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class SubActivity2 extends Activity {
private Button button3;
private String username,password,confirm_password;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub2);
button3=(Button)findViewById(R.id.button3);
final EditText usernameET,passwordET;
final EditText confirm_passwordET;
usernameET =(EditText)findViewById(R.id.edit_name);
passwordET = (EditText)findViewById(R.id.edit_pwd);
confirm_passwordET = (EditText)findViewById(R.id.confirm_edit_pwd);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username =usernameET.getText().toString().trim();
password = passwordET.getText().toString().trim();
confirm_password = confirm_passwordET.getText().toString().trim();
if(TextUtils.equals(password,confirm_password)) {
String msg = "您输入的用户名是" + usernameET.getText() + "\n" + "密码是" + confirm_passwordET.getText();
Toast toast = Toast.makeText(SubActivity2.this, msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Intent intent = new Intent();
//intent.putExtra("username",username);
//intent.putExtra("password",password);
//setResult(111,intent);
//finish();
}else{
String msg = "密码不一致!请重新输入!";
Toast toast = Toast.makeText(SubActivity2.this, msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
});
}
}
RecycleViewActivity.java
package com.example.myapplication1;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class RecycleViewActivity extends AppCompatActivity {
List<AlbumInfo> mData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_view);
//视图
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.babyAlbumRecycleView_bt);
//数据
mData = getData();
AlbumAdapter adapter = new AlbumAdapter(this,mData);
//桥接
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//设置itemAnimator
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
private List<AlbumInfo> getData()
{
List< AlbumInfo> list = new ArrayList<>();
int i;
for (i= 0;i<3;i++)
{
AlbumInfo album1 = new AlbumInfo("毡帽系列1", "此系列服装有点cute,像不像小车夫。", R.drawable.i1);
list.add(album1);
AlbumInfo album2 = new AlbumInfo("毡帽系列2", "此系列服装有点cute,像不像小车夫。", R.drawable.i2);
list.add(album2);
AlbumInfo album3 = new AlbumInfo("毡帽系列3", "此系列服装有点cute,像不像小车夫。", R.drawable.i3);
list.add(album3);
AlbumInfo album4 = new AlbumInfo("毡帽系列4", "此系列服装有点cute,像不像小车夫。", R.drawable.i4);
list.add(album4);
AlbumInfo album5 = new AlbumInfo("毡帽系列5", "此系列服装有点cute,像不像小车夫。", R.drawable.i5);
list.add(album5);
AlbumInfo album6 = new AlbumInfo("毡帽系列6", "此系列服装有点cute,像不像小车夫。", R.drawable.i6);
list.add(album6);
}
return list;
}
}
AlbumAdapter.java
package com.example.myapplication1;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {
//缓存子项布局中的子控件
private LayoutInflater mInflater;
private List<AlbumInfo> mData;
//缓存子项布局中的子控件
static class ViewHolder extends RecyclerView.ViewHolder {//内部类,保存子项item布局实例,子项第一次出现时调用
private ImageView img;
private TextView title;
private TextView info;
private ImageButton btn;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.album_thumb);
title = (TextView) itemView.findViewById(R.id.album_title);
info = (TextView) itemView.findViewById(R.id.album_info);
btn = (ImageButton) itemView.findViewById(R.id.album_btn);
}
}
//构造函数,传递数据源
public AlbumAdapter(Context context, List<AlbumInfo> list){
mInflater = LayoutInflater.from(context);
mData = list;
}
//创建ItemView并缓存
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.cardview_bt,parent,false);
final ViewHolder holder = new ViewHolder(view);
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
showInfo(position,v.getContext());
}
});
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = "What a lovely baby!";
Toast toast;
toast = Toast.makeText(v.getContext(),msg,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
});
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
AlbumInfo data = mData.get(position);
Toast toast;
toast = Toast.makeText(v.getContext(),"相册标题:"+data.getTitle(),Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
});
holder.info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
AlbumInfo data = mData.get(position);
Toast toast;
toast = Toast.makeText(v.getContext(),"相册详情:"+data.getInfo(),Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
});
return holder;
}
//绑定数据到itemview显示
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
AlbumInfo data = mData.get(position);
holder.img.setImageResource(data.getThumbId());
holder.title.setText(data.getTitle());
holder.info.setText(data.getInfo());
}
@Override
public int getItemCount() {//子项一共有多少项
return mData.size();
}
private void showInfo(int position,Context context){
AlbumInfo data = mData.get(position);
new AlertDialog.Builder(context)
.setTitle(data.getTitle())
.setMessage(data.getInfo())
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
AlbumInfo.java
package com.example.myapplication1;
public class AlbumInfo {
private String title;
private String info;
private int thumbId;
public AlbumInfo(String title, String info, int thumbId) {
this.title = title;
this.info = info;
this.thumbId = thumbId;
}
public String getTitle() {
return title;
}
public String getInfo() {
return info;
}
public int getThumbId() {
return thumbId;
}
}
activity_main.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="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal|center_vertical"
tools:context=".MainActivity"
android:background="@drawable/background_login"
>
<LinearLayout
android:layout_width="225dp"
android:layout_height="351dp"
android:background="@drawable/background_login_div"
android:orientation="vertical">
<TextView
android:layout_width="223dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="WELCOME"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edit_name_login"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/edit_login"
android:hint="请输入用户名" />
<EditText
android:id="@+id/edit_pwd_login"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/edit_login"
android:hint="请输入密码"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="记住密码" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="90dp"
android:layout_height="33dp"
android:layout_weight="1"
android:background="@drawable/background_button_div"
android:text="login"
android:textSize="16sp"
/>
<Button
android:id="@+id/button2"
android:layout_width="90dp"
android:layout_height="33dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="@drawable/background_button_div"
android:text="register"
android:textSize="16sp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<!--suppress ALL -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/jimi1"
android:scaleType="fitXY"
/>
</LinearLayout>
activity_sub1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="200dp"
android:layout_weight="1"
android:text="欢迎进入DIY!"
android:textAlignment="center"
android:textSize="24sp"
android:textColor="@color/colorAccent"
/>
</LinearLayout>
acitivity_sub2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/signup_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/signup_msg"
android:textSize="25sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/signup_msg"
android:layout_marginTop="30dp"
android:hint="@string/editText5"
android:singleLine="true" />
<EditText
android:id="@+id/edit_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/editText6"
android:inputType="textPassword"
android:layout_below="@id/edit_name"
/>
<EditText
android:id="@+id/confirm_edit_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edit_pwd"
android:layout_marginTop="2dp"
android:hint="@string/editText7"
android:inputType="textPassword"
tools:ignore="Autofill" />
<TextView
android:id="@+id/sex_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex_msg"
android:layout_below="@id/confirm_edit_pwd"
android:layout_marginTop="8dp"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/confirm_edit_pwd"
android:layout_toRightOf="@id/sex_msg"
android:orientation="horizontal">
<RadioButton
android:id="@+id/sex_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/sex_male" />
<RadioButton
android:id="@+id/sex_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex_female" />
</RadioGroup>
<TextView
android:id="@+id/academic_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/academic_text"
android:layout_below="@id/sex_msg"
android:layout_marginTop="10dp"/>
<Spinner
android:id="@+id/academic_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sex_msg"
android:layout_marginStart="7dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="8dp"
android:layout_toEndOf="@+id/academic_text"
android:layout_toRightOf="@+id/academic_text"
android:entries="@array/academic"
android:fadeScrollbars="true"
android:prompt="@string/academic_prompt"
android:scrollIndicators="right"
android:spinnerMode="dialog">
</Spinner>
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hobby_msg"
android:layout_below="@id/academic_msg">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hobby_swim"
android:text="游泳"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hobby_music"
android:text="音乐"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hobby_book"
android:text="读书"
/>
</LinearLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button3"
android:layout_centerHorizontal="true"
android:layout_below="@id/hobby_msg"
/>
</RelativeLayout>
activity_recycle_view.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"
tools:context=".RecycleViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/babyAlbumRecycleView_bt"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
cardview_bt.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
app:cardCornerRadius="5dp"
app:cardElevation = "2dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/album_thumb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="@+id/album_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp" />
<TextView
android:id="@+id/album_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
<ImageButton
android:id="@+id/album_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="@android:color/transparent"
android:src="@drawable/btn" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
strings.xml
<resources>
<string name="app_name">Application</string>
<string name="editText5">用户名</string>
<string name="editText6">密码</string>
<string name="editText7">请确认密码</string>
<string name="signup_msg">注册</string>
<string name="sex_msg">性别</string>
<string name="sex_male">男</string>
<string name="sex_female">女</string>
<string name="button3">submit</string>
<string name="academic_text">学历</string>
<string name="academic_prompt">请选择</string>
<string-array name="academic">
<item>博士</item>
<item>硕士</item>
<item>大学</item>
<item>高中</item>
</string-array>
</resources>