2013-06-02 90 views
2

我目前正在將活動轉換爲片段。該活動包含一個Edittext,單擊時顯示一個用於選擇日期的DialogFragmment。在活動中,我會簡單地調用它來顯示對話框:在片段中顯示DialogFragment

@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

我用你的代碼開發的一個例子,改變爲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> 
+0

工程很棒。非常感謝。 – mgnusl

+0

@mgnusl不客氣!我很樂意幫助你。 – mari

1

您的代碼爲我工作的代碼,就沒有錯誤顯示方法。檢查是否導入正確的庫(例如,如果使用支持庫,則爲android.support.v4.app。*)。

編輯 - 完整的源代碼:

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)); 
    } 
} 
+0

似乎運作相當好。謝謝! – mgnusl

+0

我將工作代碼添加到我的答案中。 – viduka