DatePickerDialog打开两次

时间:2019-04-07 11:36:34

标签: java android datepicker ontouchlistener

我要在编辑文本中以以下格式显示日期:YYYY-MM-DD。但是我有一些问题。 首先,当我触摸日期的“编辑文本”时,键盘仍处于打开状态。我想摆脱它。 然后,日期选择对话框出现两次。如果我设置了一次,则再次出现对话框选择器。当我第二次设置日期时,则在“编辑文本”中设置日期。 我使用片段。 第一个片段附加在第一个活动上,第二个片段附加在第二个活动上。

第二个片段代码:

package com.example.week9_fragment_task;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.Calendar;


public class SecondFragment extends Fragment
{
    private Button btnadd,btncancel;
    private Spinner spinner;
    private EditText date;
    Calendar calendar=Calendar.getInstance();
    DBTools dbTools;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnadd=(Button)getActivity().findViewById(R.id.btn_add_fragment2);
        btncancel=(Button)getActivity().findViewById(R.id.btn_cancel_fragment2);
        spinner=(Spinner)getActivity().findViewById(R.id.spinner);
        date=(EditText)getActivity().findViewById(R.id.datepicker);

        ArrayAdapter adapter=ArrayAdapter.createFromResource(getActivity(),R.array.health_type,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        date.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Date_Alert(v);
                return false;
            }

            private void Date_Alert(View v)
            {

                DatePickerDialog datePickerDialog=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
                    {
                        date.setText(year+"-"+month+"-"+dayOfMonth);
                    }

                    },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }
        });

第二个片段XML代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_gravity="center"
    tools:context=".SecondFragment">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <EditText
            android:id="@+id/txtname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:hint="@string/Hint_Name"
            android:inputType=""
            android:importantForAutofill="no"
            android:autofillHints="" tools:targetApi="o" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="150dp"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/datepicker"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="date"
            android:autofillHints=""
            android:hint="@string/datepicker"
            android:importantForAutofill="no" tools:targetApi="o" />

        <EditText
            android:id="@+id/timepicker"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="time"
            android:hint="@string/timepicker"
            android:autofillHints="" tools:targetApi="o" />
</LinearLayout>

</FrameLayout>

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:1)

    date.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Date_Alert(v);
                    return true;
                }

                private void Date_Alert(View v)
                {

    getActivity().getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
    );                
    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        //date_str = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
                        mYear = year;
                        mMonth = monthOfYear;
                        mDay = dayOfMonth;
                        mMonthName = MONTHS[monthOfYear];
                        Log.d("before", "onDateSet: " + mYear + "" + mMonthName + "" + mDay);
                        start_date_str = mDay + " " + mMonthName + " " + mYear;

                    }
                }, mYear, mMonth, mDay);
        datePickerDialog.show();
}