NumberPicker的用法
说明:NumberPicker:数字选择器,是3.0以后引入的组件,可以通过其做些数字的滑动选择,也可以用其做一些定时器的应用,该篇是平时学习中的一些心得和小结,以及写得一些小例子,第一次写博客,如有错误请给予指正。
Java代码中:
1.声明NumberPicker控件
2.获得NumberPicker的对象以及设置相关的参数:
setMaxValue();//设置最大显示
setMinValue();//设置最小显示
setValue(); //设置当前显示
setFocusable(true);//控件设置成可获取焦点状态
setFocusableInTouchMode(true);//触摸是否能获得焦点
3.设置监听器Listener
setOnValueChangedListener(OnValueChangeListener
onValueChangeListener );
比如例子中设置的是:setOnValueChangedListener(numChangedListener);
4.NumberPicker.OnValueChangeListener
其函数 public void onValueChange(NumberPicker arg0, int arg1, int arg2)
//arg1:oldValue 前一个选中的值;arg2:newValue当前选中的值。
例子中:
private NumberPicker.OnValueChangeListener numChangedListener = new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
num = numberpicker1.getValue();
switch (num) {
}
}
};
实例代码:星座判断
MainActivity.java
public class MainActivity extends Activity {
//numberpicker1 简单的NumberPicker的操作
private NumberPicker numberpicker1;
//numberpicker2,numberpicker3是星座month和day
private NumberPicker numberpicker2;
private NumberPicker numberpicker3;
private ImageView image;
private TextView text;
private TextView text1;
private int num;
int month;
int day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberpicker1 = (NumberPicker) findViewById(R.id.numberpicker1);
numberpicker1.setMaxValue(38);
numberpicker1.setMinValue(1);
numberpicker1.setValue(num);
numberpicker1.setFocusable(true);
numberpicker1.setFocusableInTouchMode(true);
numberpicker1.setOnValueChangedListener(numChangedListener);
/*/
* setMaxValue() 设置最大值
* setMaxValue() 设置最小值
* setValue() 设置当前值
* setFocusable(true) 设置可获得焦点
* setFocusableInTouchMode(true) 设置触摸可以获得焦点
*/
numberpicker2 = (NumberPicker) findViewById(R.id.numberpicker2);
numberpicker2.setMaxValue(12);
numberpicker2.setMinValue(1);
numberpicker2.setValue(month);
numberpicker2.setFocusable(true);
numberpicker2.setFocusableInTouchMode(true);
numberpicker2.setOnValueChangedListener(monthChangedListener);
/*/
* setMaxValue根据每月的天数不一样,使用switch()进行分别判断
*/
numberpicker3 = (NumberPicker) findViewById(R.id.numberpicker3);
numberpicker3.setMinValue(1);
numberpicker3.setValue(day);
numberpicker3.setFocusable(true);
numberpicker3.setFocusableInTouchMode(true);
numberpicker3.setOnValueChangedListener(dayChangedListener);
image = (ImageView) findViewById(R.id.image);
text = (TextView) findViewById(R.id.text);
text1 = (TextView) findViewById(R.id.textxing);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private NumberPicker.OnValueChangeListener numChangedListener = new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
num = numberpicker1.getValue();
switch (num) {
case 1:
image.setBackgroundResource(R.drawable.a);
text.setText("李");
break;
case 2:
image.setBackgroundResource(R.drawable.b);
text.setText("狗");
break;
case 3:
image.setBackgroundResource(R.drawable.c);
text.setText("蛋");
break;
default:
break;
}
}
};
private NumberPicker.OnValueChangeListener monthChangedListener = new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
month = numberpicker2.getValue();
xingzuo();
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numberpicker3.setMaxValue(31);
break;
case 2:
numberpicker3.setMaxValue(29);
break;
case 4:
case 6:
case 9:
case 11:
numberpicker3.setMaxValue(30);
break;
default:
break;
}
}
};
private NumberPicker.OnValueChangeListener dayChangedListener = new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
day = numberpicker3.getValue();
xingzuo();
}
};
// 对获得的month 和 day 进行处理
void xingzuo() {
if ((month == 1 && day > 19) || (month == 2 && day < 19)) {
text1.setText("水瓶座");
} else if ((month == 2 && day > 18) || (month == 3 && day < 21)) {
text1.setText("双鱼座");
}
else if ((month == 3 && day > 20) || (month == 4 && day < 20)) {
text1.setText("白羊座");
} else if ((month == 4 && day > 19) || (month == 5 && day < 21)) {
text1.setText("金牛座");
} else if ((month == 5 && day > 20) || (month == 6 && day < 22)) {
text1.setText("双子座");
} else if ((month == 6 && day > 21) || (month == 7 && day < 23)) {
text1.setText("巨蟹座");
} else if ((month == 7 && day > 22) || (month == 8 && day < 23)) {
text1.setText("狮子座");
} else if ((month == 8 && day > 22) || (month == 9 && day < 23)) {
text1.setText("处女座");
} else if ((month == 9 && day > 22) || (month == 10 && day < 24)) {
text1.setText("天秤座");
} else if ((month == 10 && day > 23) || (month == 11 && day < 23)) {
text1.setText("天蝎座");
} else if ((month == 11 && day > 22) || (month == 12 && day < 22)) {
text1.setText("射手座");
} else if ((month == 12 && day > 21) || (month == 1 && day < 20)) {
text1.setText("摩羯座");
}
}
}
activity_main.xml 布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/numberpicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="47dp"
android:layout_marginTop="20dp" />
android:id="@+id/text"
android:layout_width="88dp"
android:layout_height="22dp"
android:layout_marginLeft="55dp"
android:layout_marginTop="20dp"
android:gravity="center" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/numberpicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp" />
android:id="@+id/numberpicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="30dp" />
android:id="@+id/textxing"
android:layout_width="88dp"
android:layout_height="22dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp" />
静态效果图:
源码下载位置 http://download.csdn.net/detail/u010881221/8298155
如何从英特尔® INDE测试版过渡到英特尔® INDE 2015版?
作者:KarthiyayiniChinnaswamy本文将为您介绍从英特尔®INDE测试版项目过渡到英特尔®INDE2015版的所需步骤。在从INDE测试版升级到INDE2015版之前,了解这两种
Mono 平台 安卓 APP 菜单栏制作
介绍安卓APP底布菜单的制作效果图如下:现在开始上Ui设计的代码:Main.axml?xmlversion=1.0encoding=utf-8?LinearLayoutxmlns:android=http://schemas.android.com/apk/res/androidandroi
android的一个小问题,activity暂停数秒后自动进入另一个activity
初学安卓,第一踩做安卓app,遇到的第一个问题就是首页显示后,停止若干秒,然后自动跳转到下一页一开始以为很简单,只要onCreate()方法里面让sleep