[英]how can i receive value from AlertDialog(class) box in MainActivity(class) without passing control to AlertDialog class(Android Development)
說明:我創建了AlertDialogBox類,當我需要彈出對話框時,我使用alertDialog.show(),對話框彈出窗口將輸入設為YES / NO(按鈕為正和負),並保持相同的活動,但是我如何獲取值,即yes或no,並通過對話框中的mainActivity用戶輸入值。 嘗試:-我嘗試使用bundle,使用put / get和鍵值,但它返回null值。 嘗試使用全局變量,但仍然為空值。
謝謝您的幫助
您可以通過使用接口以及DialogFragment
類的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
}
}
主要活動:
私有無效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類
公共類AlertDialogFragment擴展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;
}
}
因此,如何在不傳遞意圖或控制的情況下,將從AlertDialogFragment類接收的“是”值傳遞給主活動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.