老规矩先上源码
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@drawable/img"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图书借阅系统"
android:textSize="32sp"
android:textColor="#DD3636"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读者姓名:"
android:textSize="20sp"
android:textColor="#977063"/>
<EditText
android:id="@+id/userName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textColor="#E19627"
android:hint="姓名"
android:textColorHint="#E19627"/>
<RadioGroup
android:id="@+id/sexGroup"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15sp"
android:text="男"
android:textColor="#B3BF35"/>
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15sp"
android:text="女"
android:textColor="#B3BF35"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="借出时间:"
android:textSize="20sp"
android:textColor="#977063"
/>
<EditText
android:id="@+id/borrowTime"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textColor="#E19627"
android:hint="如2000:00:00"
android:textColorHint="#E19627"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="归还时间:2022-6-8"
android:textColor="#E19627"
android:textSize="20sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="偏好:"
android:textSize="20sp"
android:textColor="#977063"
android:layout_marginTop="9dp"/>
<CheckBox
android:id="@+id/histroy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="历史"
android:textSize="20sp"
android:textColor="#977063"/>
<CheckBox
android:id="@+id/suspense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="悬疑"
android:textSize="20sp"
android:textColor="#977063"/>
<CheckBox
android:id="@+id/art"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文艺"
android:textSize="20sp"
android:textColor="#977063"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="年龄:"
android:textSize="20sp"
android:textColor="#977063"
/>
<TextView
android:id="@+id/mAge"
android:layout_width="35dp"
android:layout_height="match_parent"
android:text="18"
android:textSize="20sp"
android:textColor="#977063"/>
<SeekBar
android:id="@+id/ageSeekBar"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:max="130"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="130"
android:textSize="20sp"
android:textColor="#977063"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/bookImage"
android:layout_width="250dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<TextView
android:id="@+id/bookName"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="书名;"
android:textSize="22sp"/>
<TextView
android:id="@+id/bookType"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="类型:"
android:textSize="22sp"/>
<TextView
android:id="@+id/ageText"
android:text="适用年龄:"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="22sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="16"
android:orientation="horizontal">
<Button
android:layout_marginLeft="50dp"
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text=" 查 找 "/>
<Button
android:id="@+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="105dp"
android:text="下一个"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
package com.example.bookdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.example.bookdemo.module.Book;
import java.text.DateFormat;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText userName;
private RadioGroup sexGroup;
private EditText borrowTime;
private CheckBox histroy;
private CheckBox suspence;//悬疑
private CheckBox art;
private SeekBar ageSeekBar;
private ImageView bookImage;
private TextView bookName;
private Button searchButton;
private Button nextButton;
private ArrayList<Book> arraylist;
private ArrayList<Book> resultList;
private TextView mAge;
private TextView bookType;
Boolean isHistroy;
Boolean isArt;
Boolean isSuspence;
String name;
String time;
String sex;
int age=0;
int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewS();
setIfo();
Listeners();
}
private void Listeners() {
userName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
name = userName.getText().toString();
}
});
sexGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.female:
sex = "女";
break;
case R.id.male:
sex = "男";
break;
}
}
});
borrowTime.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
time = borrowTime.getText().toString();
if (time.compareTo("2022:06:08")>=0){
Toast.makeText(MainActivity.this, "时间晚于归还时间", Toast.LENGTH_SHORT).show();
}
}
});
histroy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
isHistroy = b;
}
});
suspence.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
isSuspence = b;
}
});
art.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
isArt = b;
}
});
ageSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
age = ageSeekBar.getProgress();
//无法使用int型
mAge.setText(new Integer(age).toString());
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resultList.clear();
index=0;
if (arraylist!=null){
for (Book book:arraylist){
if ((book.getArt()==isArt)||(book.getHistroy()==isHistroy)||(book.getSuspence()==isSuspence)){
resultList.add(book);
}
}
}
if (index<resultList.size()){
bookImage.setImageResource(resultList.get(index).getPic());
Toast.makeText(MainActivity.this, name+","+resultList.get(index).getBookName(), Toast.LENGTH_SHORT).show();
bookName.setText(resultList.get(index).getBookName());
bookType.setText(resultList.get(index).getType());
}
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (index<resultList.size()-1){
index++;
bookImage.setImageResource(resultList.get(index).getPic());
Toast.makeText(MainActivity.this, name+","+resultList.get(index).getBookName(), Toast.LENGTH_SHORT).show();
bookName.setText(resultList.get(index).getBookName());
bookType.setText(resultList.get(index).getType());
}
else{
Toast.makeText(MainActivity.this, "没有啦", Toast.LENGTH_SHORT).show();
}
}
});
}
private void setIfo() {
arraylist = new ArrayList<>();
//Boolean suspence, Boolean histroy, Boolean art, String bookName, int pic)
arraylist.add(new Book(false, true, true, "人生感悟", R.drawable.aa, "哲学"));
arraylist.add(new Book(false, false, true, "边城", R.drawable.bb, "文学"));
arraylist.add(new Book(true, false, false, "saplr", R.drawable.cc, "编程"));
arraylist.add(new Book(false, true, false, "光辉岁月", R.drawable.dd, "历史"));
arraylist.add(new Book(false, true, true, "宋词三百首", R.drawable.ee, "文学"));
arraylist.add(new Book(false, true, true, "中国古代文学史教学纲要", R.drawable.ff, "文学"));
arraylist.add(new Book(true, false, false, "无花果", R.drawable.gg, "悬疑"));
arraylist.add(new Book(false, false, true, "古镇记忆", R.drawable.hh, "文学"));
resultList = new ArrayList<>();
}
private void findViewS() {
userName = findViewById(R.id.userName);
sexGroup = findViewById(R.id.sexGroup);
borrowTime = findViewById(R.id.borrowTime);
histroy = findViewById(R.id.histroy);
suspence = findViewById(R.id.suspense);
art = findViewById(R.id.art);
ageSeekBar = findViewById(R.id.ageSeekBar);
bookImage = findViewById(R.id.bookImage);
bookName = findViewById(R.id.bookName);
bookType=findViewById(R.id.bookType);
searchButton = findViewById(R.id.searchButton);
nextButton = findViewById(R.id.nextButton);
mAge=findViewById(R.id.mAge);
}
}
这次学到了
setText();
一般用String类型赋值不能用int
这里我的解决办法是用Integer
mAge.setText(new Integer(age).toString());
当然用int+""也行。