我正在努力将Activity转换为片段。该活动包含一个Edittext,单击该文本时会显示用于选择日期的DialogFragmment。在Activity中,我只需调用它来显示对话框:
@SuppressLint("NewApi")
public void showDatePickerDialog(View v) {
DatePickerFragment newFragment = new DatePickerFragment(datePickerEditText);
newFragment.show(getFragmentManager(), "datePicker");
}
然而,这似乎无法显示片段中的对话框。我在“show”方法上遇到错误。所以要清楚我想知道如何从片段中显示DialogFragment?
这是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));
}
}
答案 0 :(得分:1)
您的代码对我有用, show 方法没有错误。检查是否导入正确的库(例如android.support.v4.app。*,如果使用支持库)。
EDIT - 完整源代码:
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainActivity.java
package com.viduka.stackoverflow;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, new AFragment());
ft.commit();
}
}
AFragment.java - 从中调用DatePickerFragment
package com.viduka.stackoverflow;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.EditText;
public class AFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showDatePickerDialog(new EditText(getActivity()));
}
public void showDatePickerDialog(View v) {
DatePickerFragment newFragment = new DatePickerFragment(new EditText(getActivity()));
newFragment.show(getFragmentManager(), "datePicker");
}
}
DatePickerFragment.java
package com.viduka.stackoverflow;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import android.widget.EditText;
@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
public EditText activity_edittext;
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));
}
}
答案 1 :(得分:0)
我使用您的代码开发了一个示例,将继承关系更改为FragmentActivity并且工作正常。试试这个:
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>