将数据从DialogFragment传递回其调用活动?

时间:2018-02-06 03:59:52

标签: java android dialogfragment

我的目标是将 DialogFragment 中的字符串传递给其调用活动。如果我理解正确,我认为需要在调用Activity中实现一个接口。我认为目的包括创建一个“监听器”方法。

当放在扩展DialogFragment的类中时,“getActivity()”似乎无法到达调用Activity。它似乎没有识别我添加的任何自定义方法。

我该如何处理?谢谢你的时间。

编辑01:也许我对某些事情视而不见,但我不明白为什么DialogFragment扩展类中的“getActivity()”方法引用了错误的对象。再次感谢。

MainActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {       //implements Something

    String dateThing;
    EditText date_EditText;
    DatePickerFragment newFragment;


    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        dateThing = "Change This";

        //This just changes the title of the ToolBar.
        getSupportActionBar().setTitle( "Date Changing Test" );

        date_EditText = findViewById( R.id.dateXML );
        date_EditText.setText( dateThing );

    }


    public void showDatePicker_Thing( View v ) {

        newFragment = new DatePickerFragment();
        newFragment.show( getFragmentManager(), "date picker" );

    }


    public void setDate_in_EditText( String date_for_Setting_the_EditText ) {

        date_EditText.setText( date_for_Setting_the_EditText );

    }

}

DatePickerFragment.java:

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.Toast;

import java.sql.Date;
import java.util.Calendar;


public class DatePickerFragment extends DialogFragment {

    Date testDate;


    @Override
    public Dialog onCreateDialog( Bundle savedInstanceState ) {

        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(), dateSetting_Listener, year, month, day );

    }


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

        public void onDateSet( DatePicker view, int year, int month, int day ) {

            testDate = Date.valueOf( view.getYear() + "-" + ( view.getMonth() +1 ) + "-"
                    + view.getDayOfMonth() );

            Toast.makeText( getActivity(), "The selected date:  " + testDate.toString(),
                    Toast.LENGTH_LONG ).show();

            Activity activityTest = getActivity();


            //************************************************************************
            //TODO  "setDate_in_EditText()" NOT FOUND AS A METHOD FROM "MainActivity".
            //getActivity().setDate_in_EditText( "test" );
            getActivity();
            //************************************************************************

        }
    };

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"

    android:orientation="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbars="vertical"
    android:showDividers="none"

    tools:context="com.moocow.datepickerfragment_test.MainActivity"
    >


    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="\nAdjustments Stuff"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold"
        />


    <EditText
        android:id="@+id/dateXML"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:onClick="showDatePicker_Thing"
        />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您需要将getActivity()投射到MainActivity,然后才能访问setDate_in_EditText()方法

((MainActivity)getActivity()).setDate_in_EditText("test");

答案 1 :(得分:0)

将DatePickerFragment设为活动中的内部类。

通过调用以下方法传递值。

此活动:

public void setDate(String strDate){

if(strDate!=null&&strDate.length()>0)
etDate.setText(strDate);

}