在片段android中显示对话框
Posted
技术标签:
【中文标题】在片段android中显示对话框【英文标题】:Display dialog in fragment android 【发布时间】:2015-01-03 18:12:18 【问题描述】:我正在使用 http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ 教程来显示 silder 菜单并且工作得很好。
现在我想知道对于一个特定的列表视图项,我需要显示带有是或否按钮的对话框。所以我是新手。谁能帮我解决这个问题?
这是我的滑块菜单的样子,如您所见,如果我单击第三项,我需要显示对话框,如图所示
【问题讨论】:
【参考方案1】:如果您正在学习本教程,请将 MainActivity 中的 displayView(int position) 方法更改为:
private void displayView(int position)
// update the main content by replacing fragments
Fragment fragment = null;
switch (position)
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("Alert");
// set dialog message
alertDialogBuilder
.setMessage("Pelase select your choice")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog,int id)
//do whatever you want to do when user clicks ok
)
.setNegativeButton("No",new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog,int id)
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
if (fragment != null)
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
else
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
【讨论】:
谢谢!这对我帮助很大!! :)以上是关于在片段android中显示对话框的主要内容,如果未能解决你的问题,请参考以下文章
Android 对话框片段和 onActivityResult