显示一个DialogFragment一个片段 [英] Showing a DialogFragment in a Fragment

查看:185
本文介绍了显示一个DialogFragment一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个活动转换成一个片段。该活动包含一个EDITTEXT,单击时显示了采摘日期DialogFragmment。在活动我会简单地调用它来显示对话框:

I'm currently working on converting an Activity into a Fragment. The activity contains a Edittext that when clicked shows a DialogFragmment for picking a date. In the Activity I would simply call this to show the dialog:

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

不过,这似乎并没有工作来展示从片段的对话。我在作秀的方法得到一个错误。所以要清楚,我想知道如何显示来自一个片段的DialogFragment?

However this doesn't seem to work to show the dialog from a Fragment. I'm getting an error on the "show" method. So to be clear I was wondering how to show a DialogFragment from a Fragment?

这是code为DialogFragment

This is the code for the 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));
}

}

推荐答案

我使用code开发的一个例子,改变为FragmentActivity继承关系和正常工作。试试这个:

I developed an example using your code, changing the inheritance relationship to FragmentActivity and works fine. Try this:

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>

这篇关于显示一个DialogFragment一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆