一个小实例——借书Demo

  • Step 1 布局文件
  • Step 2 初始化控件
  • Step 3 初始化数据
    • 第一步 创建数据model
    • 第二步 初始化数据
  • Step 4 添加监听器 实现基本功能
  • Step 5 查找Button 添加点击事件
  • Step 6 下一个Button 添加点击事件
  • Step 6 测试结果图
    • GitHub地址

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"><TextViewandroid: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"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:layout_width="0dp"android:layout_weight="1"android:layout_gravity="center"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="读 者 :"android:textSize="18sp" /><EditTextandroid:id="@+id/mEditName"android:layout_width= "150dp"android:layout_height="wrap_content"/></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_weight="1"android:layout_gravity="center"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="性别 :"android:textSize="18sp" /><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/male"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:textSize="16dp"/><RadioButtonandroid:id="@+id/female"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"android:textSize="16dp"/></RadioGroup></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="借出时间 :"android:textSize="16sp" /><EditTextandroid:id="@+id/borrowedTime"android:layout_width= "110dp"android:layout_height="wrap_content"android:inputType="datetime"/></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_weight="1"android:layout_gravity="center"android:layout_height="wrap_content"><TextViewandroid: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><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="爱 好 :"android:textSize="18sp" /><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="历史"android:textSize="18sp"android:layout_marginLeft="16dp"android:id="@+id/isHistory" /><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="悬疑"android:textSize="18sp"android:id="@+id/isHorror" /><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="文艺"android:textSize="18sp"android:id="@+id/isArt" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="年 龄 :"android:textSize="18sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0"android:layout_marginLeft="50dp"android:textSize="16sp" /><SeekBarandroid:id="@+id/seekBar"android:layout_width="186dp"android:layout_height="match_parent"android:progress="18"android:max="100" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="100"android:textSize="16sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#11223344"android:layout_marginTop="10dp"android:orientation="horizontal"><ImageViewandroid:id="@+id/bookPic"android:layout_width="0dp"android:layout_height="150dp"android:layout_weight="1"android:src="@drawable/ic_launcher_foreground"/><LinearLayoutandroid:layout_width="0dp"android:layout_height="150dp"android:layout_weight="1"android:orientation="vertical"><TextViewandroid: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"/><TextViewandroid: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"/><TextViewandroid: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><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="10dp"><Buttonandroid:id="@+id/search_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查找"android:background="#e1bee7"/><TextViewandroid:id="@+id/showTip"android:layout_width="100dp"android:layout_height="wrap_content"android:textSize="16sp"android:gravity="center_vertical"android:text="无"/><Buttonandroid: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;@Overrideprotected 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() {//初始化bookListbookList = 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() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {if(reader!=null){reader.setName(s.toString());}}});mradioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic 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() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic 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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {isHistory = isChecked;}});horrorCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {isHorror = isChecked;}});artCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {isArt= isChecked;}});ageSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {age = seekBar.getProgress();reader.setAge(age);Toast.makeText(MainActivity.this,"年龄:"+age,Toast.LENGTH_SHORT).show();}});searchButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {searchBook();}});nextButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {}});
}

Step 5 查找Button 添加点击事件

private void searchBook() {//将resultBook清空if(bookResult == null)  bookResult = new ArrayList<>();bookResult.clear();currentIndex= 0;//遍历所有书籍for(int index = 0; index<bookList.size();index++){//将符合条件的书籍放在resultBook里面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));}}}//设置显示index = 0 的第一本数据if(currentIndex<bookResult.size()){bookImageView.setImageResource(bookResult.get(currentIndex).getPic());//设置searchTip中的内容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() {@Overridepublic void onClick(View v) {currentIndex++;if(currentIndex<bookResult.size()){bookImageView.setImageResource(bookResult.get(currentIndex).getPic());//设置searchTip中的内容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.

一个小实例——借书Demo相关推荐

  1. html class和id,css教程之样式表的基本语法(二) class(类)和id的一个小实例

    class(类)和id的一个小实例 在上一节中我们了解了如何为特定的标签定义样式,例如我们利用 "h1{font-size: 12px;}"将页面内所有的标题1的字体大小改为了12 ...

  2. Jmeter Web 性能测试入门 (四):一个小实例带你学会 Jmeter 脚本编写

    测试场景: 模拟并发100个user,在TesterHome 站内搜索VV00CC 添加线程组 添加HTTP信息头管理器 添加HTTP Sampler 填写HTTP Sampler中的信息 添加监听器 ...

  3. 使用Bootstrap框架写的一个小实例

    今天学习了一下Bootstrap框架,,写一个小实例练练手,感受一下bootstrap兼容三端的强大. <!doctype html> <html lang="zh-CN& ...

  4. 杜邦接口还有一个跟他相反的叫什么_C++|一个小实例理解STL六大部件

    STL主要包含容器(containers).迭代器(iterators).空间配置器(allocator).配接器(adapters,或叫适配器).算法(algorithms).仿函数(functor ...

  5. 企业内部借书小程序---借书驿站

    微信搜一搜借书驿站 借书驿站小程序使用初体验 功能特色 页面截图 借书驿站小程序使用初体验 刚进入小程序,有新手操作视频 功能特色 注册即可使用 ,在小程序上面注册后就可以使用,无需等待审核: 上传图 ...

  6. 一个小的UGM的Demo

    翻译自[https://www.cs.ubc.ca/~schmidtm/Software/UGM/small.html] 在这个demo中,我们使用一个非常简单的无向图模型(UGM)来表示一个简单的概 ...

  7. 每天一个小实例——使用pdfplumber提取pdf表格及文本,并保存到excel

    pdfplumber简介 (1)可以方便地获取pdf的各种信息,包括文本.表格.图表.尺寸等,它不支持修改或生成pdf,也不支持对pdf扫描件的处理 (2)pdfplumber中有两个基础类,PDF和 ...

  8. 第八周拓展实践1小明借书

    问题及代码: /*Copyright(c)2016,烟台大学计算机学院 all rights reserved. 作者:曹欣宇 完成日期:2016年10月22日 版本号:v1.0题目描述小明要把5本书 ...

  9. java实用教程——组件及事件处理——布局的一个小实例

    import javax.swing.*; import java.awt.*;public class BasicComponentDemo {Frame frame = new Frame(&qu ...

最新文章

  1. shell python优势_python的优势
  2. windows10安装docker
  3. Winform开发框架之插件化应用框架实现
  4. 【原理+实战+视频+源码】docker权限参数
  5. linux下添加apt-get命令,Ubuntu Linux系统下apt-get命令整理
  6. 四款机型全面开售 海蓝色iPhone 12 Pro最受欢迎
  7. SQL Server provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错
  8. 半兽人野性重新觉醒 当初掘金将其放弃 如今野兽感恩回报火箭
  9. 深入理解计算机系统第六章家庭作业之6.35 6.36
  10. WiFi Interface 的 name 如何获取到
  11. Datalogic得利捷Memor™ 10入选“安卓企业推荐计划”
  12. 邻接矩阵实现(有向邻接矩阵)、(无向邻接矩阵) 基于C++
  13. python输入五个数并求平均值、保留一位小数_程序功能要实现输入理财产品金额和存款天数,计算预计收益金额(保留1位小数)。...
  14. dnw for linux
  15. C++实验5 游戏玩家类Player、两个道具类Helm和Armor
  16. 【python】math函数库介绍及其例题
  17. Windows 10 升级软件 Windows 10 易升
  18. 论文查重:降低重复率技巧
  19. 有关相位噪声的Offset Frequency
  20. ArcGis空间分析学习:土地利用动态变化分析

热门文章

  1. 哈工大软件构造lab2---实验心得
  2. 【基于Android聊天软件开发-哔哩哔哩】 https://b23.tv/109B82i
  3. 鸿蒙系统服务器在哪,鸿蒙系统我的服务怎么打开、关闭?鸿蒙系统我的服务添加设置教程...
  4. windows下爬虫+数据库(非关系数据库/关系数据库)的实现简介1-redis
  5. 物联网开源项目:机智云智能婴儿摇篮,可跟踪、能防丢
  6. 一伪淘宝html网页
  7. Program type already present问题解决
  8. 网游创业失败全攻略[转帖]
  9. 干货!14个最新优质加载动画设计,让等待成为一种享受
  10. android代码检测anr,ANR检查定位分析工具