Android之旅第五站——日期对话框dialog …

手机设置中会有对日期的设置,这就和现在说的dialog差不多。

效果图:

不多说,附代码了解:

package com.example.dialog1;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button bt;
    boolean flag=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

    }

    private void initViews() {
        // TODO Auto-generated method stub
        bt = (Button) findViewById(R.id.bt);
    }

    public void myclick(View v) {
        DatePickerDialog dpd=new DatePickerDialog(this, new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // TODO Auto-generated method stub

                flag=!flag;
                if(flag)
                Toast.makeText(MainActivity.this, "你选择的日期是:"+year+"/"+monthOfYear+"/"+dayOfMonth, 1000).show();;
            }
        }, 2017, 2, 4);
        dpd.setIcon(R.drawable.ic_launcher);
        dpd.setTitle("日期:");
        dpd.setMessage("请选择日期:");
        dpd.show();

    }



    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
  • 1

日期对话框可以通过构造方法获得,不用像之前单选多选那样通过builder来获取,

new DatePickerDialog(context, callBack, year, monthOfYear, dayOfMonth)

context:上下文this

callBack: 实际上也算是监听事件,当做监听事件就好了,可以new出来。

后面3个是显示界面那会默认的年月日。

大家也可以看到,为什么我会用flag,其实,不知道是android的一个bug还是什么,我没搞懂,每次提示的时候回提示两次,当

然你做log打印也会是两次打印,所以我在刚开始就定义一个boolean的flag,当第一次flag为true显示,下个语句立马改成false

就可以避免打印2次了。

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context="com.example.dialog1.MainActivity" >

    <Button 
       android:id="@+id/bt"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="riqi"
       android:onClick="myclick"
        />



</RelativeLayout>
  • 1

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

apple_51426592

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值