本人菜鸟一枚,第一次写技术博客,有点小紧张,加上代码很乱,求不喷。

先放效果图:

 界面就这么简单。



然后放上代码:

package com.example.dialog;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.NumberPicker.OnValueChangeListener; public class MainActivity extends Activity {
public Context context = MainActivity.this;
String msg = "test";
// numberpicker1 简单的NumberPicker的操作
private NumberPicker numberpicker1; // numberpicker2,numberpicker3是星座month和day
private NumberPicker numberpicker2;
private NumberPicker numberpicker3;
private NumberPicker np;
private TextView text;
private TextView text1;
private int num;
int month =1;
int day =1;
String cityy;
private String[] city= {"北京","上海","广州","深圳","成都","天津"};
private Button bt1;
private Button bt2;
private Builder dialog;
private Builder dialog2;
private TextView tv1;
private TextView tv2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 =(Button) findViewById(R.id.bt1);
tv1 = (TextView) findViewById(R.id.tv1);
bt2 =(Button) findViewById(R.id.bt2);
tv2 = (TextView) findViewById(R.id.tv2);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
dialog = createLoadingDialog(MainActivity.this, "test");
dialog.create().show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
cityy ="北京";
dialog2 = createLoadingDialog2(MainActivity.this, "test");
dialog2.create().show();
}
}); } /**
* 得到自定义的progressDialog
*
* @param context
* @param msg
* @return
*/
public Builder createLoadingDialog(Context context, String msg) { LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view numberpicker2 = (NumberPicker) v.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) v.findViewById(R.id.numberpicker3);
numberpicker3.setMinValue(1);
numberpicker3.setMaxValue(31);
numberpicker3.setValue(day);
numberpicker3.setFocusable(true);
numberpicker3.setFocusableInTouchMode(true);
numberpicker3.setOnValueChangedListener(dayChangedListener);
text1 = (TextView) v.findViewById(R.id.textxing); AlertDialog.Builder loadingDialog = new AlertDialog.Builder(context);
loadingDialog.setMessage("生日");
loadingDialog.setView(v);
loadingDialog.setCancelable(false);// 不可以用“返回键”取消
loadingDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
tv1.setText(month + "月" + day + "日" +"\t" + text1.getText().toString());
}
});
loadingDialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
return loadingDialog; } public Builder createLoadingDialog2(Context context, String msg) { LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.loading_dialog2, null);// 得到加载view
np = (NumberPicker) v.findViewById(R.id.numberpicker22);
np.setDisplayedValues(city);
np.setMinValue(0);
np.setMaxValue(city.length - 1);
np.setOnValueChangedListener(cityChangedListener); AlertDialog.Builder loadingDialog = new AlertDialog.Builder(context);
loadingDialog.setMessage("城市");
loadingDialog.setView(v);
loadingDialog.setCancelable(false);// 不可以用“返回键”取消
loadingDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
tv2.setText(cityy);
}
});
loadingDialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
cityy = city[0];
}
});
return loadingDialog; } 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();
} };
private NumberPicker.OnValueChangeListener cityChangedListener = new OnValueChangeListener() { @Override
public void onValueChange(NumberPicker arg0, int arg1, int arg2) {
//获得城市名
String [] a = np.getDisplayedValues();
cityy = a[arg2];
} }; // 对获得的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("摩羯座");
} }
}

代码很简单,主要是把NumberPicker跟数据绑定在一起,就不多解释了。还有布局太简单,也不用贴出来了吧。大体就这样

Android使用NumberPicker控件实现选择城市,生日的更多相关文章

  1. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  2. 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用

    Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...

  3. 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用

    Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...

  4. 【风马一族_Android】第4章Android常用基本控件

    第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...

  5. 【ALearning】第三章 Android基本常见控件

    本章主要介绍主要的寻常较多使用的控件,包含TextView.EditView.ImageView.Button等.本章将介绍相关控件基本属性的使用,为以后章节的进阶学习提供基础.案例中引用的Linea ...

  6. [开源]在iOS上实现Android风格的控件Toast

    [开源]在iOS上实现Android风格的控件Toast iOS的风格和Apple其他产品一样,简单而粗暴.没有给人其他选择的余地,让你又爱又恨.同样的,Apple对待iOS平台的开发人员和对待大众消 ...

  7. [置顶] Android常用适配器控件

    Android常用适配器控件 列表控件用于显示数据集合,Android不是使用一种类型的控件管理显示和数据,而是将这两项功能分布用列表控件和适配器来实现.列表控件扩展了android.widget.A ...

  8. Android 一个日历控件的实现代码

    转载  2017-05-19   作者:Othershe   我要评论 本篇文章主要介绍了Android 一个日历控件的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看 ...

  9. Android 自己定义控件开发入门(二)

    上一次我们讲了一堆实现自己定义控件的理论基础.列举了View类一些能够重写的方法,我们对这些方法的重写是我们继承View类来派生自己定义控件的关键 我通过一个最简单的样例给大家展示了这一个过程,不管是 ...

随机推荐

  1. jQuery源码学习:使用隐藏的new来创建对象

    在JQuery源码中发现,JQuery定义一个类,但不用new关键字去创建该类对象,而使用方法调用()方式去创建该对象. 很多时候我们是这样写类,然后使用new创建对象的: function Pers ...

  2. mac Word 怎样放大缩小文档结构图文字大小

    在文档结构图的侧栏里按住control+option,然后滑动鼠标滚轮/双指上下滚动触摸板.

  3. PHP PSR-4 Autoloader 自动加载(中文版)

    Autoloader 关键词 “必须”("MUST").“一定不可/一定不能”("MUST NOT").“需要”("REQUIRED"). ...

  4. Find them, Catch them(POJ 1703 关系并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38668   Accepted: ...

  5. 1007 Numerical Summation of a Series

    简单入门题.按照题目给的指导编程,算多少数要理解题意. #include <stdio.h> int main(){ int k,ssx; double x,psix; ;ssx<= ...

  6. c# vs2010 winfrom控件检测网络环境

    写下以作备用,代码附上. public partial class UserControl1 : UserControl, IObjectSafety { //检测网络状态 [DllImport(&q ...

  7. 给FPGA初学者的建议——不要浮躁(转)

    原作者:http://hi.baidu.com/zhoubin7020 作为FPGA新人常常在论坛上问,用什么语言啊,用什么软件啊,用那个公司的芯片等问题.我个人认为学习FPGA和学习单片机有相同的道 ...

  8. redis的安装与配置

    官网 http://redis.io/download 管理工具 http://docs.redisdesktop.com/en/latest/quick-start/ https://redisde ...

  9. 自定义searchview的编辑框,搜索按钮,删除按钮,光标等

    //指定某个私有属性 Field mSearchHintIconField = argClass.getDeclaredField("mSearchHintIcon"); mSea ...

  10. C语言随笔_return答疑

    1. 例子,看实例2-2.这道题有同学会问,那个return有什么用?这么讲吧,return是个英文单词,中文意思是“返回”,用在程序里也是返回的意思,返回啥呢?返回一个值.在func函数中,retu ...