我的代码其他功能都正常,唯独“判断借出时间”那里,一输入借出时间就停止运行,这段代码该如何更改?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | 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积分~
来为老师/同学的回答评分吧