这次主要是悬系的下拉框Spinner和数字转轮NumberPicker的使用。先分析相关的用到的知识点。

在Android中,用string-array是一种简单的提取XML资源文件数据的方法。
  例子如下:

  把相应的数据放到values文件夹的arrays.xml文件里

<?xml version="1.0" encoding="utf-8"?>
  <resources>
   <string-array name="city">
   <item>丰台区</item>
   <item>石景山区</item>
   <item>海淀区</item>
   <item>漳州市</item>
   <item>朝阳区</item>
   </string-array>
  </resources>

  然后在Activity里,直接使用
  Resources res =getResources();
  String[] city=res.getStringArray(R.array.city);
  即可取得string-array name="city"下的所有item数据,简单又方便的一种方法

对于

private AdapterView.OnItemSelectedListener spnSexOnItemSelected=new AdapterView.OnItemSelectedListener() {}
中的相关参数的知识点:
  1. parent 发生选中事件的 AbsListView。
  2. view AbsListView 中被选中的视图。
  3. position 视图在一览中的位置(索引)。
  4. id 被点击条目的行 ID。
private AdapterView.OnItemSelectedListener spnSexOnItemSelected=new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
msSex=parent.getSelectedItem().toString();
}是用来确定下拉框中对应的被选中的哪一项。
具体相关的实现代码如下:
MainActivity.java
package com.example.myapplicationhome;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { private NumberPicker mNumPickerAge;
private Button mBtnOk;
private TextView mTxtR,mTxtAge;
private Spinner mSpnSex;
private String msSex; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mTxtAge=(TextView)findViewById(R.id.txtAge);
mTxtAge.setText("25"); mNumPickerAge=(NumberPicker)findViewById(R.id.numPickerAge); mNumPickerAge.setMinValue(0);
mNumPickerAge.setMaxValue(200);
mNumPickerAge.setValue(25); mNumPickerAge.setOnValueChangedListener(numPickerAgeOnValueChange); mBtnOk=(Button)findViewById(R.id.btnOk);
mTxtR=(TextView)findViewById(R.id.txtR); mBtnOk.setOnClickListener(btnOkOnClick);
mSpnSex=(Spinner)findViewById(R.id.spnSex);
mSpnSex.setOnItemSelectedListener(spnSexOnItemSelected); } private View.OnClickListener btnOkOnClick =new View.OnClickListener(){
@Override
public void onClick(View v) {
int iAge=mNumPickerAge.getValue();
String strSug=getString(R.string.result); if(msSex.equals(getString(R.string.sex_male)))
{
if(iAge<)
strSug+=getString(R.string.sug_not_hurry);
else if(iAge>33)
strSug+=getString(R.string.sug_get_married);
else
strSug+=getString(R.string.sug_find_couple);
}
else
{
if(iAge<)
strSug+=getString(R.string.sug_not_hurry);
else if(iAge>30)
strSug+=getString(R.string.sug_get_married);
else
strSug+=getString(R.string.sug_find_couple);
}
mTxtR.setText(strSug);
}
}; private AdapterView.OnItemSelectedListener spnSexOnItemSelected=new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
msSex=parent.getSelectedItem().toString();
} @Override
public void onNothingSelected(AdapterView<?> parent) { }
};
private NumberPicker.OnValueChangeListener numPickerAgeOnValueChange=new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
mTxtAge.setText(String.valueOf(newVal));
}
};
}

strings.xml

<resources>
<string name="app_name">婚姻建议程序</string>
<string name="sex">性别:</string>
<string name="age">年龄:</string>
<string name="btn_ok">确定</string>
<string name="result">最终诊断结果:</string>
<string name="edt_age_hint">(输入年龄)</string>
<string name="sug_not_hurry">还不急</string>
<string name="sug_get_married">你废了!</string>
<string name="sug_find_couple">开始找对象。</string>
<string name="sex_male">男</string>
<string-array name="sex_list">
<item>男</item>
<item>女</item>
</string-array>
<string name="spn_sex_list_prompt">请选择性别</string> </resources>

activaty_main.xml

<?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"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:id="@+id/sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sex"
android:textSize="25sp" />
<Spinner
android:id="@+id/spnSex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/sex_list"
android:spinnerMode="dialog"
android:prompt="@string/spn_sex_list_prompt" />
<TextView
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/age"
android:textSize="25sp" />
<TextView
android:id="@+id/txtAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="25sp" />
<NumberPicker
android:id="@+id/numPickerAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left" />
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#4CAF50"
android:text="@string/btn_ok" /> <TextView
android:id="@+id/txtR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/result"
android:textSize="25sp" />
</LinearLayout>

具体的实验结果如下:

最新文章

  1. Java的HTTP通信
  2. javascript基础04
  3. javascript 中的 let 作用域
  4. MMORPG大型游戏设计与开发(概述)updated
  5. REST API (from IBM)
  6. 使用context来传递数据,一个context是一系列变量
  7. mysql 远程连接速度慢的解决方案
  8. python中的面向对象编程
  9. jQ $.extend用法
  10. jQuery选择
  11. NServiceBus 入门2
  12. 经验之谈——gulp使用教程
  13. 201521123045 《Java程序设计》第4周学习总结
  14. 【CF802C】Heidi and Library(网络流)
  15. JMeter3.0 post参数/BeanShell中文乱码问题
  16. HDMI SCDC处理过程
  17. keySet,entrySet用法 以及遍历map的用法
  18. 写脚本时出现: Permission denied
  19. vue--todolist的实现
  20. JProfiler 简要使用说明

热门文章

  1. Ajax的封装,以及利用jquery的ajax获取天气预报
  2. Natas27 Writeup(mysql溢出截断漏洞)
  3. XSS构造剖析
  4. 写了个python脚本,循环执行某一个目录下的jmeter脚本————解决的问题,每次回归时,都得一个个拉取
  5. babel-loader配置
  6. 新手版超详细LoadRunner12完整安装+汉化过程
  7. RNN,GRU,LSTM
  8. Redis 按正则获取keys
  9. 六、深浅拷贝与while循环
  10. HDU-1051 一个DP问题