我試圖展示一個DialogFragment
詢問用戶他的名字,我需要找回這個字符串到我的主要活動。DialogFragments和活動,傳遞信息
我已經創建了以下在Android引導的定製對話框片段:http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
在我的代碼,以響應正面按鈕,用戶點擊,我能夠調用一個函數在我的主要活動:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.textdialog, null)).setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int id) {
((mainActivity)getActivity()).doPositiveClick();
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
((mainActivity)getActivity()).doNegativeClick();
}
});
return builder.create();
}
其中R.layout.textdialog是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/textdraw"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="introduce text" />
</LinearLayout>
我的問題是,我怎麼能得到用戶輸入的文本在我的對話片段把它發回我的主要活動?
謝謝!
感謝,該解決方案是有意義的,並且效果很好。 – henry000