从编辑文本启动DatePicker

时间:2013-10-05 02:30:31

标签: android datepicker

我在其中一个 edittext

上使用了日期选择器

编辑文字的ID :: android:id="@+id/DATE_EDIT_TEXT_ID"

search_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#E1E1E1"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="1" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="City"
            android:textSize=" 15dp"
            android:textStyle="bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@drawable/rounded_edittext" />
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#E1E1E1"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="1" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date"
            android:textSize=" 15dp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/DATE_EDIT_TEXT_ID"
            android:layout_width="258dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@drawable/rounded_edittext"
            android:onClick="selectDate" />


    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#E1E1E1"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="2.5" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Type"
            android:textSize=" 15dp"
            android:textStyle="bold" />

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="2dp"
                android:layout_weight=".5"
                android:background="@drawable/yourbuttonbackground"
                android:button="@android:color/transparent"
                android:checked="true"
                android:gravity="center_horizontal"
                android:padding="5dp"
                android:text="Breakfast" />

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="2dp"
                android:layout_weight=".5"
                android:background="@drawable/yourbuttonbackground"
                android:button="@android:color/transparent"
                android:gravity="center_horizontal"
                android:padding="5dp"
                android:text="Lunch" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="2dp"
                android:layout_weight=".5"
                android:background="@drawable/yourbuttonbackground"
                android:button="@android:color/transparent"
                android:gravity="center_horizontal"
                android:padding="5dp"
                android:text="Dinner" />
        </RadioGroup>
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="254dp"
        android:background="#E1E1E1"
        android:onClick="selectDate"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="1" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="38dp"
            android:background="@drawable/blue_button"
            android:text="Search" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#E1E1E1"
        android:orientation="horizontal" >

        <include layout="@layout/screen_bottombar_photodesc" />
    </RelativeLayout>

</LinearLayout>

SearchPage.java

public class SearchPage extends FragmentActivity {

    EditText mEdit;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_page);
    }

    public void selectDate(View view) {
        DialogFragment newFragment = new SelectDateFragment();
        newFragment.show(getSupportFragmentManager(), "DatePicker");
    }
    public void populateSetDate(int year, int month, int day) {
        mEdit = (EditText)findViewById(R.id.DATE_EDIT_TEXT_ID);
        mEdit.setText(day+"/"+month+"/"+year);
    }
    public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, yy, mm, dd);
        }

        public void onDateSet(DatePicker view, int yy, int mm, int dd) {
            populateSetDate(yy, dd, mm+1);
        }
    }

}

  • 当我点击编辑文本时 android:id="@+id/DATE_EDIT_TEXT_ID"日期选择器应该启动
  • 但发生的事情是我点击屏幕日期选择器的地方 正在推出

如何解决此问题

1 个答案:

答案 0 :(得分:0)

// try this
public class SearchPage extends FragmentActivity {

    EditText mEdit;
    DialogFragment newFragment;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_page);
        mEdit = (EditText)findViewById(R.id.DATE_EDIT_TEXT_ID);
        newFragment = new SelectDateFragment();
        mEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               newFragment.show(getFragmentManager(), "DatePicker");
            }
        });
    }

    public void populateSetDate(int year, int month, int day) {
        mEdit.setText(day+"/"+month+"/"+year);
    }
    public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, yy, mm, dd);
        }

        public void onDateSet(DatePicker view, int yy, int mm, int dd) {
            populateSetDate(yy, dd, mm+1);
        }
    }

}