我正在尝试使用单例获取DialogFragment
并获得Dialog
,但是当尝试获取此对象时,您的属性不会得到此内容。
我希望获得Dialog
的视图,但这不会访问布局的ID,这是我的DialogFragment
:
public static class newTaskFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.new_task_dialog, null))
// Add action buttons
.setPositiveButton(R.string.create_task, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
newTaskFragment.this.getDialog().cancel();
}
});
return builder.create();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
DialogSingleton.getInstance().setDialog(this.getDialog());
super.onViewCreated(view, savedInstanceState);
}
}
这是我的单身人士:
DialogSingleton.java
public class DialogSingleton {
private static DialogSingleton Instance;
private Dialog mDialog;
private DialogSingleton(){
mDialog = new Dialog(MainActivity.ma.getApplicationContext());
}
public static DialogSingleton getInstance(){
if(Instance == null)
{
Instance = new DialogSingleton();
}
return Instance;
}
public Dialog getDialog(){
return this.mDialog;
}
public void setDialog(Dialog value){
mDialog = value;
}
}
但是当我尝试访问该对象时,但是没有访问视图Button,当我尝试访问Singleton时代码如何:
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Time time = new Time();
time.hour = hourOfDay;
time.minute = minute;
/*Fragment fragment = getFragmentManager().findFragmentByTag("newTask");
View viewv = fragment.getView();*/
Log.d("TaskManager", "A classe é" + DialogSingleton.getInstance().getDialog());
Button button = (Button) DialogSingleton.getInstance().getDialog().findViewById(R.id.choice_data);
if(button != null) button.setText("ss");
}
}
总是按钮返回null,现在请有人,有什么不对,非常感谢。
更新
new_task_dialog.xml
..
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/pick_date"
android:onClick="showDatePickerDialog"
android:id="@+id/choice_data"
/>
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="@string/choice_hour"
android:onClick="showTimePickerDialog"
android:id="@+id/choice_hour"
/>
..
答案 0 :(得分:0)
在布局文件中定义一个Button,并将其ID指定为“choice_data”。它应该像
<Button
android:id="@+id/choice_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/>
findViewById(R.id.data_choice)
在无法找到ID为
答案 1 :(得分:0)
您需要设置对话框的视图。目前,您的对话框在布局中找不到指定的按钮。因此,您需要先提供布局,然后按住按钮。
public class DialogSingleton {
// set the view
public void setView(int layoutId){
mDialog.setContentView(layoutId);
}
}
首先将自定义布局设置为对话框,然后从该视图中获取按钮。
编辑:
解决方案1
public static class TimePickerFragment extends newTaskFragment implements TimePickerDialog.OnTimeSetListener {...}
解决方案2
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
// some code here
Dialog d = DialogSingleton.getInstance().getDialog();
d.setContentView(R.layout.new_task_dialog);
Button b = (Button)d.findViewById(R.id.choice_data);