package com.gtercn.myapplication;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast; import java.text.SimpleDateFormat;
import java.util.Calendar; public class MainActivity extends Activity {
private Button mButton;
private NumberPicker mCityPicker;
private NumberPicker mCityPicker2;
private NumberPicker mCityPicker3;
private final static String[] CITYS = {"2004", "2006", "2003", "2002", "2001", "2016"};
private final static String[] date = {"5", "6", "7", "8", "9", "10"};
private final static String[] monteh = {"12", "11", "10", "9", "8", "7"};
private int mSeletedIndex;
private int mSeletedIndex2;
private int mSeletedIndex3;
String txt1;
String txt2;
String txt3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder7 = new AlertDialog.Builder(
MainActivity.this);
builder7.setTitle("标题");
builder7.setIcon(R.mipmap.ic_launcher);
View view = LayoutInflater.from(MainActivity.this).inflate(
R.layout.activity_main, null);
mCityPicker = (NumberPicker) view.findViewById(R.id.city_picker);
mCityPicker2 = (NumberPicker) view.findViewById(R.id.city_picker2);
mCityPicker3 = (NumberPicker) view.findViewById(R.id.city_picker3);
//设置数据源
mCityPicker.setDisplayedValues(CITYS);
//设置数据长度
mCityPicker.setMinValue(0);
mCityPicker.setMaxValue(CITYS.length - 1);
mCityPicker.setValue(4);
//设置监听
mCityPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
mSeletedIndex = newVal;
}
});
mCityPicker.setOnScrollListener(new NumberPicker.OnScrollListener() {
@Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
switch (scrollState) {
case NumberPicker.OnScrollListener.SCROLL_STATE_FLING:
Log.e("TAG", "SCROLL_STATE_FLING");
//惯性滑动
break;
case NumberPicker.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
//手动滑动
Log.e("TAG", "SCROLL_STATE_TOUCH_SCROLL");
break;
case NumberPicker.OnScrollListener.SCROLL_STATE_IDLE:
//停止滑动
Log.e("TAG", "SCROLL_STATE_IDLE");
txt1 = CITYS[mSeletedIndex].toString();
//mTextView.setText(CITYS[mSeletedIndex]); //Toast.makeText(MainActivity.this, CITYS[mSeletedIndex], Toast.LENGTH_SHORT).show();
break;
} }
});
//设置数据源
mCityPicker2.setDisplayedValues(monteh);
//设置数据长度
mCityPicker2.setMinValue(0);
mCityPicker2.setMaxValue(monteh.length - 1);
mCityPicker2.setValue(4);
//设置监听
mCityPicker2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
mSeletedIndex2 = newVal;
}
});
mCityPicker2.setOnScrollListener(new NumberPicker.OnScrollListener() {
@Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
switch (scrollState) {
case NumberPicker.OnScrollListener.SCROLL_STATE_FLING:
Log.e("TAG", "SCROLL_STATE_FLING");
//惯性滑动
break;
case NumberPicker.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
//手动滑动
Log.e("TAG", "SCROLL_STATE_TOUCH_SCROLL");
break;
case NumberPicker.OnScrollListener.SCROLL_STATE_IDLE:
//停止滑动
Log.e("TAG", "SCROLL_STATE_IDLE");
txt2 = monteh[mSeletedIndex2];
break;
} }
});
//设置数据源
mCityPicker3.setDisplayedValues(date);
//设置数据长度
mCityPicker3.setMinValue(0);
mCityPicker3.setMaxValue(date.length - 1);
mCityPicker3.setValue(4);
//设置监听
mCityPicker3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
mSeletedIndex3 = newVal;
}
});
mCityPicker3.setOnScrollListener(new NumberPicker.OnScrollListener() {
@Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
switch (scrollState) {
case NumberPicker.OnScrollListener.SCROLL_STATE_FLING:
Log.e("TAG", "SCROLL_STATE_FLING");
//惯性滑动
break;
case NumberPicker.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
//手动滑动
Log.e("TAG", "SCROLL_STATE_TOUCH_SCROLL");
break;
case NumberPicker.OnScrollListener.SCROLL_STATE_IDLE:
//停止滑动
Log.e("TAG", "SCROLL_STATE_IDLE");
txt3 = date[mSeletedIndex3];
break;
} }
});
builder7.setView(view);
builder7.setPositiveButton("确定",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(
MainActivity.this,
txt1 + txt2 + txt3, Toast.LENGTH_LONG)
.show();
}
});
builder7.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
builder7.create();
builder7.show();
}
});
}
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal"
android:gravity="center_vertical"> <NumberPicker
android:id="@+id/city_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></NumberPicker> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal"
android:gravity="center_vertical"> <NumberPicker
android:id="@+id/city_picker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></NumberPicker> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="月"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal"
android:gravity="center_vertical"> <NumberPicker
android:id="@+id/city_picker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></NumberPicker> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout> </RelativeLayout>

先附上效果图片

Dialog+NumberPicker的更多相关文章

  1. 3java面试题 传智 发的 有用

    第一章内容介绍 20 第二章JavaSE基础 21 一.Java面向对象 21 1. 面向对象都有哪些特性以及你对这些特性的理解 21 2. 访问权限修饰符public.private.protect ...

  2. 使用NumberPicker定制自己喜欢的Date&TimePicker

    1.NumberPicker简介: NumberPicker是Android3.0之后引入的一个控件,主要功能是用于选择一组预定义好数字的控件. 该控件主要需要指导一个用于监听当前value变化的li ...

  3. Android使用NumberPicker控件实现选择城市,生日

    本人菜鸟一枚,第一次写技术博客,有点小紧张,加上代码很乱,求不喷. 先放效果图: 界面就这么简单. 然后放上代码: package com.example.dialog; import java.ut ...

  4. 底部菜单实现(Dialog方案)

    项目中经常会要实现在屏幕底部弹出一个窗口,比如一个分享窗口: 下面详解实现步骤: 1.定义布局 <?xml version="1.0" encoding="utf- ...

  5. Android日期对话框NumberPicker的使用方法教程

    NumberPicker是Android3.0之后引入的一个控件.NumberPicker 是用于选择一组提前定义好数字的控件.比方时间hour的选择仅仅有0-23有效,则能够通过setMinValu ...

  6. 使用MonoTouch.Dialog简化iOS界面开发

    MonoTouch.Dialog简称MT.D,是Xamarin.iOS的一个RAD工具包.它提供易于使用的声明式API,不需要使用导航控制器.表格等ViewController来定义复杂的应用程序UI ...

  7. 2000条你应知的WPF小姿势 基础篇<78-81 Dialog/Location/WPF设备无关性>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000ThingsYou Should Know About C# 和 2,00 ...

  8. Android PopupWindow Dialog 关于 is your activity running 崩溃详解

    Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...

  9. 三个不常用的HTML元素:<details>、<summary>、<dialog>

    前面的话 HTML5不仅新增了语义型区块级元素及表单类元素,也新增了一些其他的功能性元素,这些元素由于浏览器支持等各种原因,并没有被广泛使用 文档描述 <details>主要用于描述文档或 ...

随机推荐

  1. Maven 3.3.3 Win10环境下的使用实例(中)

    继上一篇文章介绍了Maven在Windows中的安装,本文将介绍 Maven 的核心概念. POM (Project Object Model) Maven 插件 Maven 生命周期 Maven 依 ...

  2. SAP系统更改小数点显示问题

    在SAP系统中会出现小数点显示的问题,比如123.12,正常情况下是这样显示,但SAP系统是德国的出的系统,德国的书写数字的习惯是将小数点“.”写成“,”逗号,显示为:123,12 这个问题可以使用事 ...

  3. 使用ssh正向连接、反向连接、做socks代理的方法

     ssh -L 219.143.16.157:58080:172.21.163.32:8080 用户名@localhost -p 10142  在 219.143.16.157机器执行   将ssh隧 ...

  4. (keymap设置及eclipse常用快捷键总结)

    设置pycharm为Eclipse快捷键后使用总结: Ctrl + O  根据name模糊查找当前文件中类.方法 Alt + (向左箭头或者向右箭头) ,回退or前进到到之前查看或者编辑处 Alt + ...

  5. 头文件algorithm中的常用函数

    非修改性序列操作(12个) 循环         对序列中的每个元素执行某操作         for_each() 查找         在序列中找出某个值的第一次出现的位置         fin ...

  6. 使用json格式输出

    /** * json输出 * * @param unknown_type $info */ public function json_out ($info) { header('Content-typ ...

  7. HTML标记语法之图片Img元素

    语法:<img src=”xxx.jpg”alt=”xxx”title=”xxx”> 属性可取值如下: 属性名称 属性值 说明 src URL 图片路径 alt 文本 图片无法显示时的文本 ...

  8. Destination Host Unreachable

    自己的Linux 机器连不上服务器了,ping XXXX的时候报这个错误了 看了一下是因为IP的原因==>进入Linux的图形界面==>System==>Administration ...

  9. hdu 2057 A+B

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2057 For each test case,print the sum of A and B in h ...

  10. 并发中的Native方法,CAS操作与ABA问题

    Native方法,Unsafe与CAS操作 >>JNI和Native方法 Java中,通过JNI(Java Native Interface,java本地接口)来实现本地化,访问操作系统底 ...