我会用什么来从DialogFragment中的MainActivity中获取TextView?

时间:2013-09-25 17:21:47

标签: android dialog textview timepicker dialogfragment

我正在编写一个简单的应用程序,打开一个时间选择器对话框,要求输入,根据系统时间检查它,并告诉你它是否正确。但是,我需要获取显示是真还是假的TextView,并在TimePickerFragment中更改它,这是一个DialogFragment。我该怎么办?

TimePickerFragment.java

package com.example.timechecker;
import java.util.Calendar;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment
                        implements TimePickerDialog.OnTimeSetListener {
int hour;
int minutes;


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    hour = c.get(Calendar.HOUR_OF_DAY);
    minutes = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, 12, 00,
            DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    //Grab the Text View from the Main Activity
//      MainActivity m = new MainActivity();
//      m.grabText text = new m.grabText();

    //Check if given Picker value is == to the system time, display whether or          not it is so
    if (hourOfDay == hour && minutes == minute) {
        text.setText("You are correct!");
    } else {
        text.setText("You are incorrect!");
    }
}
}

MainActivity.java

package com.example.timechecker;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
//Creates the dialog with the Time Picker
public void checkTime(View view) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");

}

//Class to grab the Text View for the Dialog Fragment
public class grabText {
    TextView text = (TextView) findViewById(R.id.whatTime);

    //Uses a method to set the Text View text
    public void setText(String string) {
        text.setText(string);
    }
}

}

2 个答案:

答案 0 :(得分:0)

当你在任何活动中调用findViewById时,我只搜索活动布局。因此,要从对话框中获取TextView,您需要对该对话框的引用,然后调用dialog.findViewById(id_of_field),这将为您提供所需的TextView。

希望这有助于

答案 1 :(得分:0)

我最近这样做了,我通过让MainActivity.java成为DialogFragment的父母来实现这一目标。

<强> MainActivity.Java

将此添加到MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    mActivity = this;

之后,将其设为全局:

public class MainActivity extends Activity {
    MainActivity mActivity;

<强> DialogFragment

将此添加到DialogFragment

public void setParent(MainActivity parent) {
    mParent = parent;
}

之后,将其设为全局:

public class ClearDialogModern extends DialogFragment {
    MainActivity mParent;

编辑:这是您setParent的所在地。将其放入MainActivity.java onCreate

newDialogFragment = new DialogFragment();
newDialogFragment.setParent(mActivity);

使用方法:

您现在可以使用mParent来引用MainActivity.java。