Step 1 布局文件
<?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:orientation="vertical"
android:background="#22ff55dd"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="借 书"
android:textColor="#c51162"
android:textSize="40sp"
android:background="#ede7f6"
android:layout_marginBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读 者 :"
android:textSize="18sp" />
<EditText
android:id="@+id/mEditName"
android:layout_width= "150dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别 :"
android:textSize="18sp" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="16dp"
/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textSize="16dp"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="借出时间 :"
android:textSize="16sp" />
<EditText
android:id="@+id/borrowedTime"
android:layout_width= "110dp"
android:layout_height="wrap_content"
android:inputType="datetime"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="返还时间 :2020.06.30"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱 好 :"
android:textSize="18sp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="历史"
android:textSize="18sp"
android:layout_marginLeft="16dp"
android:id="@+id/isHistory" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="悬疑"
android:textSize="18sp"
android:id="@+id/isHorror" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文艺"
android:textSize="18sp"
android:id="@+id/isArt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年 龄 :"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginLeft="50dp"
android:textSize="16sp" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="186dp"
android:layout_height="match_parent"
android:progress="18"
android:max="100" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#11223344"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/bookPic"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:src="@drawable/ic_launcher_foreground"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="@+id/bookName"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="@string/bookName"
/>
<TextView
android:id="@+id/bookStyle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="@string/bookStyle"
/>
<TextView
android:id="@+id/bookSuitableAge"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="@string/bookSuitableAge"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查找"
android:background="#e1bee7"/>
<TextView
android:id="@+id/showTip"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:gravity="center_vertical"
android:text="无"
/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一个"
android:background="#e1bee7"/>
</LinearLayout>
</LinearLayout>
Step 2 初始化控件
public class MainActivity extends AppCompatActivity {
private EditText readName;
private RadioGroup mradioGroup;
private RadioButton maleRadio;
private RadioButton femaleRadio;
private TextView borrowTime;
private CheckBox historyCheckBox;
private CheckBox horrorCheckBox;
private CheckBox artCheckBox;
private SeekBar ageSeekBar;
private ImageView bookImageView;
private TextView mBookName;
private TextView mBookStyle;
private TextView mBookSuitableAge;
private Button searchButton;
private TextView searchTip;
private Button nextButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
readName = findViewById(R.id.mEditName);
mradioGroup = findViewById(R.id.radioGroup);
maleRadio = findViewById(R.id.male);
femaleRadio = findViewById(R.id.female);
borrowTime = findViewById(R.id.borrowedTime);
historyCheckBox = findViewById(R.id.isHistory);
horrorCheckBox = findViewById(R.id.isHorror);
artCheckBox = findViewById(R.id.isArt);
ageSeekBar = findViewById(R.id.seekBar);
bookImageView = findViewById(R.id.bookPic);
mBookName = findViewById(R.id.bookName);
mBookStyle = findViewById(R.id.bookStyle);
mBookSuitableAge = findViewById(R.id.bookSuitableAge);
searchButton= findViewById(R.id.search_button);
searchTip = findViewById(R.id.showTip);
nextButton = findViewById(R.id.next_button);
}
}
Step 3 初始化数据
第一步 创建数据model
public class Book {
private String name ;
private String style;
private int suitableAge;
private int pic;
private boolean isHistory;
private boolean isHorror;
private boolean isArt;
public Book() {
}
public Book(String name, String style, int suitableAge, int pic, boolean isHistory, boolean isHorror, boolean isArt) {
this.name = name;
this.style = style;
this.suitableAge = suitableAge;
this.pic = pic;
this.isHistory = isHistory;
this.isHorror = isHorror;
this.isArt = isArt;
}
setter &getter method
......
}
public class Reader {
private String name;
private String sex;
private int age;
private String dataTime;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDataTime() {
return dataTime;
}
public void setDataTime(String dataTime) {
this.dataTime = dataTime;
}
}
第二步 初始化数据
private void initData() {
bookList = new ArrayList<>();
bookList.add(new Book("人生感悟","人生/哲学",20,R.drawable.aa,true,false,true));
bookList.add(new Book("边城","剧情/文艺",30,R.drawable.bb,true,true,true));
bookList.add(new Book("sapir","未知",20,R.drawable.cc,false,false,true));
bookList.add(new Book("光辉岁月","剧情/人生",40,R.drawable.dd,true,false,true));
bookList.add(new Book("宋词三百首","诗词",10,R.drawable.ee,true,false,false));
bookList.add(new Book("荷花","散文",20,R.drawable.f,true,false,true));
bookList.add(new Book("中国古代文学","文学",30,R.drawable.ff,true,false,true));
bookList.add(new Book("无花果","剧情",40,R.drawable.gg,false,true,true));
bookList.add(new Book("古镇记忆","散文",35,R.drawable.hh,true,true,false));
reader = new Reader();
bookResult = new ArrayList<>();
}
Step 4 添加监听器 实现基本功能
private void setListener() {
readName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(reader!=null){
reader.setName(s.toString());
}
}
});
mradioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.male:
reader.setSex("男");
break;
case R.id.female:
reader.setSex("女");
break;
default:
break;
}
}
});
borrowTime.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(reader!=null){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date data=null;
borrowTime1 = s.toString();
try {
data= format.parse(borrowTime1);
reader.setDataTime(format1.format(data));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
});
historyCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isHistory = isChecked;
}
});
horrorCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isHorror = isChecked;
}
});
artCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isArt= isChecked;
}
});
ageSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
age = seekBar.getProgress();
reader.setAge(age);
Toast.makeText(MainActivity.this,"年龄:"+age,Toast.LENGTH_SHORT).show();
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchBook();
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
Step 5 查找Button 添加点击事件
private void searchBook() {
if(bookResult == null) bookResult = new ArrayList<>();
bookResult.clear();
currentIndex= 0;
for(int index = 0; index<bookList.size();index++){
if(bookList.get(index).getSuitableAge()<age
){
if( bookList.get(index).isHistory()==isHistory
|| bookList.get(index).isHorror()==isHorror
|| bookList.get(index).isArt()==isArt){
bookResult.add(bookList.get(index));
}
}
}
if(currentIndex<bookResult.size()){
bookImageView.setImageResource(bookResult.get(currentIndex).getPic());
mBookName.setText(bookResult.get(currentIndex).getName());
mBookStyle.setText(bookResult.get(currentIndex).getStyle());
mBookSuitableAge.setText(bookResult.get(currentIndex).getSuitableAge()+"");
searchTip.setText("符合条件的书共有"+bookResult.size()+"本");
Toast.makeText(MainActivity.this,"姓名:"+reader.getName()+",性别:"+reader.getSex()+",年龄:"+reader.getAge()+
",借出时间:"+reader.getDataTime(),Toast.LENGTH_SHORT).show();
}
}
Step 6 下一个Button 添加点击事件
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentIndex++;
if(currentIndex<bookResult.size()){
bookImageView.setImageResource(bookResult.get(currentIndex).getPic());
mBookName.setText(bookResult.get(currentIndex).getName());
mBookStyle.setText(bookResult.get(currentIndex).getStyle());
mBookSuitableAge.setText(bookResult.get(currentIndex).getSuitableAge()+"");
searchTip.setText("符合条件的书共有"+bookResult.size()+"本");
Toast.makeText(MainActivity.this,"姓名:"+reader.getName()+",性别:"+reader.getSex()+",年龄:"+reader.getAge()+
",借出时间:"+reader.getDataTime(),Toast.LENGTH_SHORT).show();
}else{
bookResult=new ArrayList<>();
Toast.makeText(MainActivity.this,"没有啦",Toast.LENGTH_SHORT).show();
}
}
});
Step 6 测试结果图
GitHub地址
传送门biubiubiu.