我的代码其他功能都正常,唯独“判断借出时间”那里,一输入借出时间就停止运行,这段代码该如何更改?
package com.example.book; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; 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.Toast; import com.example.book.model.Book; import com.example.book.model.Person; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class MainActivity extends AppCompatActivity { private EditText person_name_EditText; private EditText borrow_time_EditText; private EditText back_time_EditText; private RadioGroup sex_radiogroup; private CheckBox history_checkbox,suspense_checkbox,art_checkbox; private SeekBar age_seekbar; private ImageView book_image; private EditText book_name_EditText; private EditText book_type_EditText; private EditText book_age_EditText; private Button find_button; private Button next_button; private List<Book> books; private List<Book> books_search; private StringBuilder book_type; private Person person; private Date borrow_time_date; private Date back_time_date; private boolean is_history = false; private boolean is_suspense = false; private boolean is_art = false; private int age; private int current_index; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化控件 findViews(); //初始化数据 person = new Person(); initData(); // 为控件添加监听器,实现控件的基本功能 setListeners(); } private void setListeners() { // 监听用户输入的姓名 person_name_EditText.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(person != null){ person.setPerson_name(s.toString()); } } }); // 监听用户选择的性别 sex_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.male_radiobutton: person.setSex("男"); break; case R.id.female_radiobutton: person.setSex("女"); break; } } }); // 监听用户输入的借出时间 borrow_time_EditText.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(person != null){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); person.setBorrow_time(s.toString()); /*try { borrow_time_date = sdf.parse(person.getBorrow_time()); back_time_date = sdf.parse("2020-02-28"); } catch (ParseException e) { e.printStackTrace(); } if(borrow_time_date.after(back_time_date)){ Toast.makeText(MainActivity.this,"借出时间超出归还时间!",Toast.LENGTH_SHORT); }*/ } } }); // 监听用户选择的爱好 history_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { is_history = isChecked; } }); suspense_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { is_suspense = isChecked; } }); art_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { is_art = isChecked; } }); // 监听用户选择的年龄 age_seekbar.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(); Toast.makeText(MainActivity.this,"年龄:"+ age,Toast.LENGTH_SHORT).show(); } }); // 查找书籍 find_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { search(); String person_name = person_name_EditText.getText().toString(); String sex = person.getSex(); String borrow_time = person.getBorrow_time(); Toast.makeText(MainActivity.this,"姓名:"+ person_name + " 年龄:"+ age + " 性别:" + sex+"\n借出时间:" + borrow_time,Toast.LENGTH_SHORT).show(); } }); //浏览下一个符合条件的书籍 next_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(next_button.isClickable()){ current_index ++; if(current_index < books_search.size()){ book_image.setImageResource(books_search.get(current_index).getPic()); book_name_EditText.setText(books_search.get(current_index).getBook_name().toString()); book_age_EditText.setText(books_search.get(current_index).getBook_age()+ " "); book_type_EditText.setText(books_search.get(current_index).getBook_type().toString()); } } } }); } private void search() { // 遍历所有书籍,找到符合条件的,就把它放在search_books集合中 // 每次查找结束,都清空集合 // 如果集合为空,先初始化 if(books_search == null){ books_search = new ArrayList<>(); } books_search.clear(); current_index = 0; for(int index = 0;index<books.size();index ++){ Book book = books.get(index); if(books != null){ if((book.getBook_age()>=age)&& (book.isHistory()==is_history ||book.isSuspense()==is_suspense ||book.isArt()==is_art)){ books_search.add(book); } } } if (current_index < books_search.size()){ book_image.setImageResource(books_search.get(current_index).getPic()); book_name_EditText.setText(books_search.get(current_index).getBook_name().toString()); book_age_EditText.setText(books_search.get(current_index).getBook_age()+ " "); book_type_EditText.setText(books_search.get(current_index).getBook_type().toString()); } } // 初始化数据的方法 private void initData() { books = new ArrayList<Book>(); books_search = new ArrayList<>(); books.add(new Book("感悟人生",25,true,false,true,R.drawable.aa ,"历史、文艺")); books.add(new Book("边城",20,true,false,true,R.drawable.bb,"历史、文艺")); books.add(new Book("spair",30,false,true,false,R.drawable.cc,"悬疑")); books.add(new Book("光辉岁月",40,true,true,false,R.drawable.dd,"历史、悬疑")); books.add(new Book("宋词三百首",5,true,false,true,R.drawable.ee,"历史、文艺")); books.add(new Book("中国古代文学",20,true,false,true,R.drawable.ff,"历史、文艺")); books.add(new Book("无花果",18,true,true,true,R.drawable.gg,"历史、悬疑、文艺")); books.add(new Book("古镇记忆",18,true,true,true,R.drawable.hh,"历史、文艺、悬疑")); } // 初始化控件的方法 private void findViews() { person_name_EditText = findViewById(R.id.person_name_EditText); sex_radiogroup = findViewById(R.id.sex_radiogroup); borrow_time_EditText = findViewById(R.id.borrow_time_EditText); back_time_EditText = findViewById(R.id.back_time_EditText); history_checkbox = findViewById(R.id.history_checkbox); suspense_checkbox = findViewById(R.id.suspense_checkbox); art_checkbox = findViewById(R.id.art_checkbox); age_seekbar = findViewById(R.id.age_seekbar); age_seekbar.setProgress(18); book_image = findViewById(R.id.picture_image); book_name_EditText = findViewById(R.id.book_name_EditText); book_type_EditText = findViewById(R.id.book_type_EditText); book_age_EditText = findViewById(R.id.book_age_EditText); find_button = findViewById(R.id.find_button); next_button = findViewById(R.id.next_button); } }
31
收起
正在回答 回答被采纳积分+1
4回答
1.Android 零基础入门
- 参与学习 人
- 提交作业 1789 份
- 解答问题 2907 个
Android大楼Java起,本阶段是Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始入门Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星