我有一个片段(片段)和一个对话框片段(RadioListAlert)。我想在警报对话框中添加“确定”和“取消”按钮。单击确定按钮时,可选项目名称传递给CarFragment。怎么办。
我的代码在这里:
RadioListAlert.java:
package com.h2o;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class RadioListAlert extends DialogFragment {
CharSequence[] tag = { "BMW", "AUDI", "MERCEDES", "FERRARI", "SKODA" };
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle("Tag Your Car").setSingleChoiceItems(tag, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), tag[which],
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
CarFragment.java:
//Call RadioListAlert class
new RadioListAlert().show(getActivity().getFragmentManager(), "Radio Alert");
请有人帮助我!!!
提前致谢
答案 0 :(得分:1)
您可以设置正面和负面按钮:
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//your code to pass a bundle to fragment
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do what you want when cancel is clicked
}
});
答案 1 :(得分:0)
您需要使用方法setNegativeButton()
和setOnPositiveButton
添加确定和取消按钮。
要将数据发送到您的片段,您需要创建一个接口来保存该数据,您的片段将实现它。它会像new RadioListAlert().setDialogListener(CarFragment.this).show(getActivity().getFragmentManager(), "Radio Alert");
在解除对话框之前,请记住在{strong>确定按钮调用onClickListener
时调用该侦听器。
修改强>
public class RadioListAlert extends DialogFragment {
CharSequence[] tag = { "BMW", "AUDI", "MERCEDES", "FERRARI", "SKODA" };
private CarListener carListener;
private String itemClicked;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle("Tag Your Car").setSingleChoiceItems(tag, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getActivity(), tag[which],Toast.LENGTH_SHORT).show();
itemClicked = (String) tag[which];
}
}).setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
if(carListener != null)
carListener.itemClicked(itemClicked);
//to dismiss the dialog after user choose an item and click ok, you can also add some validation before dismissing the dialog
dismiss();
}
});
return builder.create();
}
public void setListener(CarListener carListener)
{
this.carListener = carListener;
}
public interface CarListener
{
void itemClicked(String text);
}
}
你的carFragment中的实现CarListner
答案 2 :(得分:0)
试试这个
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
final View mView = inflater.inflate(R.layout.myalert, null);
builder.setView(mView);
final RadioGroup radioGroup = (RadioGroup) mView.findViewById(R.id.radioGroup1);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int checkedId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) mView.findViewById(checkedId);
Log.d("alert", checkedId+"");
--------------- your code---------------
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
--------------- your code---------------
}
});
builder.show();
您的自定义提醒对话框布局为:myalert
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>