2

我目前正在将 Activity 转换为 Fragment。该活动包含一个 Edittext,单击该文本会显示一个用于选择日期的 DialogFragmment。在活动中,我会简单地调用它来显示对话框:

@SuppressLint("NewApi")
public void showDatePickerDialog(View v) {
    DatePickerFragment newFragment = new DatePickerFragment(datePickerEditText);
    newFragment.show(getFragmentManager(), "datePicker");
}

但是,这似乎无法显示片段中的对话框。我在“显示”方法上遇到错误。所以要清楚,我想知道如何从片段中显示 DialogFragment?

这是 DialogFragment 的代码

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

public EditText activity_edittext;

@SuppressLint("ValidFragment")
public DatePickerFragment(EditText edit_text) {
    activity_edittext = edit_text;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
    activity_edittext.setText(String.valueOf(day ) + "/" +   String.valueOf(month + 1) + "/" + String.valueOf(year));
}

}

4

2 回答 2

1

您的代码对我有用,显示方法没有错误。检查是否导入正确的库(例如 android.support.v4.app.* 如果您使用支持库)。

编辑 - 完整的源代码:

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MainActivity.java

package com.viduka.stackoverflow;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(R.id.container, new AFragment());
    ft.commit();
    }
}

AFragment.java - 从中​​调用 DatePickerFragment

package com.viduka.stackoverflow;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.EditText;

public  class AFragment extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showDatePickerDialog(new EditText(getActivity()));
    }

    public void showDatePickerDialog(View v) {
        DatePickerFragment newFragment = new DatePickerFragment(new EditText(getActivity()));
        newFragment.show(getFragmentManager(), "datePicker");
    }
}

DatePickerFragment.java

package com.viduka.stackoverflow;

import java.util.Calendar;    
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import android.widget.EditText;

@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    public EditText activity_edittext;

    public DatePickerFragment(EditText edit_text) {
        activity_edittext = edit_text;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        activity_edittext.setText(String.valueOf(day) + "/"
                + String.valueOf(month + 1) + "/" + String.valueOf(year));
    }
}
于 2013-06-02T15:06:08.820 回答
1

我使用您的代码开发了一个示例,将继承关系更改为 FragmentActivity 并且工作正常。尝试这个:

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = (EditText) this.findViewById(R.id.activity_edittext);
    editText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDatePickerDialog();
        }
    });

}

public void showDatePickerDialog() {
    DatePickerFragment newFragment = new DatePickerFragment(editText);
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:orientation="vertical">

    <EditText android:id="@+id/activity_edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="text"/>

</LinearLayout>
于 2013-06-02T15:57:14.753 回答