【问题标题】:Android DatePicker showing Full Screen on InitializationAndroid DatePicker 在初始化时显示全屏
【发布时间】:2013-05-09 15:05:46
【问题描述】:

我已关注this tutorial 将日期选择器集成到我的应用程序中,我已经根据我的需要定制了原始版本并设法使其正常工作。不幸的是,作者并没有深入探讨实际使用 DatePicker 对话框的概念。

因此,应用程序启动时会显示全屏日历,而我只是想在单击“选择日期”按钮时拉出日、月和年,我需要删除什么?

代码在这里:

public class MainActivity extends Activity implements OnClickListener,
        OnCheckedChangeListener {

    TabHost th;

    Button  changeDate;


    TextView displayDate;
    public static final int dDialogId = 1;
    // date and time
    private int mYear;
    private int mMonth;
    private int mDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Set activity to full screen!!
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        th = (TabHost) findViewById(R.id.tabhost);

        th.setup();
        TabSpec specs = th.newTabSpec("tag1");
        // one tab
        specs.setContent(R.id.tab1);
        // what appears on actual tab
        specs.setIndicator("Tools");
        th.addTab(specs);
        specs = th.newTabSpec("tag2");
        // one tab
        specs.setContent(R.id.tab2);
        // what appears on actual tab
        specs.setIndicator("Calorie Calculator");
        th.addTab(specs);
        specs = th.newTabSpec("tag3");
        // one tab
        specs.setContent(R.id.tab3);
        // what appears on actual tab
        specs.setIndicator("Your Stats!");
        th.addTab(specs);

        initializeVariables();
}


    private void initializeVariables() {

        mapARun = (Button) findViewById(R.id.btnMapARun);
        weightConverter = (Button) findViewById(R.id.btnWeightConverter);

        changeDate = (Button) findViewById(R.id.btnChangeDate);
        displayDate = (TextView) findViewById(R.id.tvDisplayDate);

        changeDate.setOnClickListener(this);

        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        updateDisplay();
    }

    @Override
    @Deprecated
    protected void onPrepareDialog(int id, Dialog dialog) {
        // TODO Auto-generated method stub
        super.onPrepareDialog(id, dialog);

        ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);

    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            updateDisplay();
        }
    };

    private void updateDisplay() {
        displayDate.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-").append(mDay).append("-")
                .append(mYear));

    }



        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.btnChangeDate:
            DatePickerDialog DPD = new DatePickerDialog(this,
                    mDateSetListener, mYear, mMonth, mDay);
            DPD.show();
            break;

        }

    }



    }

}

如果您在代码运行时遇到语法错误,我深表歉意,我删掉了很多其他不相关的函数,

当调用 对话框 时,是否有另一种方法只显示日期选择器?

【问题讨论】:

    标签: java android xml dialog datepicker


    【解决方案1】:

    只需按钮即可创建您的主要类别。 使用您的代码使按钮调用另一个活动。 您将获得一个带有按钮的屏幕,该按钮调用另一个带有全屏日期选择器的屏幕。

    选择日期后,让 DatePicker Activity 自行结束。 确保使用 startActivityForResult 调用活动并使用 finishWithResult 结束。

    【讨论】:

    • 有没有办法只设置按钮在选择时调用“迷你”日期选择器,应用程序的设置方式我真的不想调用整个单独的活动来设置日期
    猜你喜欢
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多