Dialog+NumberPicker

先附上效果图片

代码展示:

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.dialog_view, 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);                //第一个NumberPicker设置数据源                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;                        }

                    }                });                //第二个NumberPicker设置数据源                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;                        }

                    }                });                //第三个NumberPicker设置数据源                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();            }        });    }}
时间: 2024-10-04 05:35:01

Dialog+NumberPicker的相关文章

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

1.NumberPicker简介: NumberPicker是Android3.0之后引入的一个控件,主要功能是用于选择一组预定义好数字的控件. 该控件主要需要指导一个用于监听当前value变化的listener.一个用于监听该控件scroll状态的listener和一个用于格式化显示该控件中value的formatter: 1) NumberPicker.OnValueChangeListener :其函数public void onValueChange(NumberPicker picke

Android日期对话框NumberPicker的用法教程

NumberPicker是Android3.0之后引入的一个控件,NumberPicker 是用于选择一组预定义好数字的控件.比如时间hour的选择只有0-23有效,则可以通过setMinValue和setMaxValue设定. 使用该控件时需注意他的两个listener和一个formatter:一个listener用于监听当前value的变化:一个listener用于监听该控件的scroll状态:formatter用于格式化显示该控件中的value.下面逐一介绍这几个接口: 1.NumberP

从零开始学android -- dialog

先看个效果图 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match

用dialog在前台实现一个简单的UI对话框

<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>对话框部件(Dialog Widget)演示</title> <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.m

android 2.x上Dialog特定情况导致无法dismiss的bug

近期在做一个简单的对话框框架,作用不过做出一个显示效果在android 2.x到android 4.x上均类似的对话框. 思路比較简单,就是使用装饰模式包裹Dialog,暴露的接口调用时会自己主动完毕一些设置 可是遇到一个特别诡异的问题,在android 2.x上的Dialog对话框无法被dismiss,仅仅要弹出来界面就卡死,强行使用eclipse又一次run的话会导致android设备黑屏无响应,假设是模拟器,则直接崩溃,看到启动画面.并且这个还不是ANR错误. 后来通过删减代码找到了问题的

weui dialog 效果

切记:weui dialog 的样式是在weui.css,而不是在weui.min.css HTML: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0" /> <

Android 对话框 Dialog

对话框 Dialog 什么是对话框 对话框是在当前的页面之上弹出的小窗口, 用于显示一些重要的提示信息, 提示用户的输入,确认信息, 或显示某种状态.如 :  显示进度条对话框, 退出提示. 对话框的特点: 1, 当前界面弹出的小窗口. 2, 用户要与它进行交互, 可以接收用户输入的信息, 也可以反馈信息给用户. 常用对话框: 1, 普通对话框 AlertDialog 2, 进度条对话框  ProgressDialog 3, 日期对话框    DatePickerDialog 4, 时间对话框

Android 弹出对话框Dialog充满屏幕宽度

final View view = LayoutInflater.from(context).inflate(layoutId, null); final Dialog dialog = new Dialog(context, R.style.style_dialog); dialog.setContentView(view); dialog.show(); Window window = dialog.getWindow(); window.setGravity(Gravity.BOTTOM)

Android 自定义Dialog类,并在Activity中实现按钮监听。

实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下: 开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的按钮,Dialog类如下,在MyDialog这个类中实现了一个LeaveMyDialogListener接口,用来实现onclick的点击事件: package com.Ieasy.Tool; import com.Ieasy.ieasyware.R; import android.app.Dialog