【问题标题】:What would I use to grab a TextView from the MainActivity within a DialogFragment?我将使用什么从 DialogFragment 中的 MainActivity 中获取 TextView?
【发布时间】:2013-09-25 17:21:47
【问题描述】:

我正在编写一个简单的应用程序,它会打开一个时间选择器对话框,要求输入,根据系统时间检查它,并告诉您它是否正确。但是,我需要获取显示它是真还是假的 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);
    }
}

}

【问题讨论】:

    标签: android dialog textview timepicker dialogfragment


    【解决方案1】:

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

    希望对你有帮助

    【讨论】:

    • 好吧,我正在尝试从主要活动中获取文本视图,所以我可以使用 TimePickerFragment 类中的变量来更改它
    【解决方案2】:

    我最近这样做了,我可以通过将 MainActivity.java 设为我的 DialogFragment 的父级来做到这一点。

    MainActivity.Java

    将此添加到MainActivity

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

    之后,在这里全局化:

    public class MainActivity extends Activity {
        MainActivity mActivity;
    

    对话框片段

    将此添加到DialogFragment:

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

    之后,在这里全局化:

    public class ClearDialogModern extends DialogFragment {
        MainActivity mParent;
    

    编辑:这里是你setParent。把这个放在你的MainActivity.javaonCreate

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

    使用方法:

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

    【讨论】:

    • 感谢您的帮助,虽然它说当我尝试mParent.grabText text = new mParent.grabText();时无法将 mParent 解析为类型
    • 更新答案,我忘了设置父级! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多