描述:我创建了 AlertDialogBox 类,当我需要弹出对话框时,我使用 alertDialog.show(),弹出对话框将输入为 YES/NO(按钮为正和负)并保持相同的活动,但是我怎样才能得到值,即是或否,并传递用户在 mainActivity 从对话框中输入的值。尝试过:- 我尝试使用捆绑包,使用 put/get 和键值,但它返回 null 值。尝试使用全局变量但仍为空值。
谢谢您的帮助
描述:我创建了 AlertDialogBox 类,当我需要弹出对话框时,我使用 alertDialog.show(),弹出对话框将输入为 YES/NO(按钮为正和负)并保持相同的活动,但是我怎样才能得到值,即是或否,并传递用户在 mainActivity 从对话框中输入的值。尝试过:- 我尝试使用捆绑包,使用 put/get 和键值,但它返回 null 值。尝试使用全局变量但仍为空值。
谢谢您的帮助
Main Activity:
private void registerClickCallBack(){
ListView list = (ListView)findViewById(R.id.starListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
StarClassValue clickedPosition = myStar.get(position);
Str = clickedPosition.getStarName();
String intr = clickedPosition.getIconNum() +"";
//Toast.makeText(MainActivity.this,Str + intr,Toast.LENGTH_LONG).show();
sendMessage(Str,intr);
}
});
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
StarClassValue click = myStar.get(position);
String str = click.getStarName();
dialog.show(getFragmentManager(),"Favourite");
return false;
}
});
}
AlertDialog Class
public class AlertDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Context context = getActivity();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Add to Favorite").setMessage("Do you want to add this artist to your favourite list").setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
return dialog;
}
}
So, how should i pass "yes" value received from AlertDialogFragment class into main activity without passing intent or control.
您可以通过使用接口和类的方法来做到getActivity()
这getParentFragment()
一点DialogFragment
。
注意:我假设(因为您没有发布任何代码)您已经使用 DialogFragment 来显示 AlertDialog。
首先创建对话框和一个特殊接口,可用于将值传递给拥有Activity
或Fragment
:
public class MyAlertDialog extends DialogFragment implements DialogInterface.OnClickListener {
public interface MyAlertDialogResultInterface {
abstract void onButtonClicked(int button);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Test message");
builder.setPositiveButton("Ok", this);
builder.setNegativeButton("No", this);
return builder.create();
}
@Override
public void onClick(DialogInterface dialogInterface, int button) {
//Check if this DialogFragment is owned by a parent fragment
if(getParentFragment() instanceof MyAlertDialogResultInterface){
((MyAlertDialogResultInterface) getParentFragment()).onButtonClicked(button);
//Else check if this DialogFragment is owned by a parent activity
} else if(getActivity() instanceof MyAlertDialogResultInterface){
((MyAlertDialogResultInterface) getActivity()).onButtonClicked(button);
}
}
}
然后将特殊界面添加到您的Activity
并使用FragmentManager
来显示对话框:
public class Test extends Activity implements MyAlertDialog.MyAlertDialogResultInterface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set up layout
}
private void showAlertDialog(){
new MyAlertDialog().show(getFragmentManager(), "dialog-tag");
}
@Override
public void onButtonClicked(int button) {
//Do what ever you want to do
}
}