我将Tabs ActionBar与Fragments一起使用。在一个 fragment 中,我有一个按钮。我想在按下按钮时显示DialogFragment
(在我的 fragment 内)。我怎样才能做到这一点?
这是我的代码
public class MainActivity extends FragmentActivity implements EditNameDialogListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//showEditDialog();
ActionBar actionbar = getActionBar();
// Tell the ActionBar we want to use Tabs.
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// initiating both tabs and set text to it.
ActionBar.Tab InboxTab = actionbar.newTab().setText("One");
ActionBar.Tab SentTab = actionbar.newTab().setText("Two");
Fragment InboxFragment = new Inbox();
Fragment SentFragment = new SentBox();
InboxTab.setTabListener(new MyTabsListener(InboxFragment));
SentTab.setTabListener(new MyTabsListener(SentFragment));
actionbar.addTab(InboxTab);
actionbar.addTab(SentTab);
}
private void showEditDialog() {
FragmentManager fm = getSupportFragmentManager();
EditNameDialog editNameDialog = new EditNameDialog();
editNameDialog.show(fm, "fragment_edit_name");
}
@Override
public void onFinishEditDialog(String inputText) {
Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public class Inbox extends Fragment implements EditNameDialogListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
Utility util = new Utility(getActivity());
View view = inflater.inflate(R.layout.inbox, container, false);
ArrayList<String[]> searchResults = util.ReadFile();
final ListView lv1 = (ListView) view.findViewById(R.id.inboxList);
lv1.setAdapter(new MyCustomBaseAdapter(getActivity(), searchResults));
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
String[] fullObject = (String[])o;
Toast.makeText(getActivity(), "You have chosen: " + " " + fullObject[0], Toast.LENGTH_LONG).show();
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_settings:
showEditDialog();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void showEditDialog() {
FragmentManager ft = getSupportFragmentManager();
EditNameDialog editNameDialog = new EditNameDialog();
editNameDialog.show(ft, "fragment_edit_name");
}
@Override
public void onFinishEditDialog(String inputText) {
Toast.makeText(getActivity(), "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
/*
* Toast.makeText(StartActivity.appContext, "Reselected!",
* Toast.LENGTH_LONG).show();
*/
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
public class EditNameDialog extends DialogFragment implements OnEditorActionListener{
public interface EditNameDialogListener {
void onFinishEditDialog(String inputText);
}
private EditText mEditText;
public EditNameDialog() {
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_name, container);
mEditText = (EditText) view.findViewById(R.id.txt_your_name);
getDialog().setTitle("Hello");
// Show soft keyboard automatically
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mEditText.setOnEditorActionListener(this);
return view;
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (EditorInfo.IME_ACTION_DONE == actionId) {
// Return input text to activity
EditNameDialogListener activity = (EditNameDialogListener) getActivity();
activity.onFinishEditDialog(mEditText.getText().toString());
this.dismiss();
return true;
}
return false;
}
public class Inbox extends Fragment implements EditNameDialogListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
Utility util = new Utility(getActivity());
View view = inflater.inflate(R.layout.inbox, container, false);
ArrayList<String[]> searchResults = util.ReadFile();
final ListView lv1 = (ListView) view.findViewById(R.id.inboxList);
lv1.setAdapter(new MyCustomBaseAdapter(getActivity(), searchResults));
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
String[] fullObject = (String[])o;
Toast.makeText(getActivity(), "You have chosen: " + " " + fullObject[0], Toast.LENGTH_LONG).show();
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_settings:
showEditDialog();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void showEditDialog() {
FragmentManager ft = getSupportFragmentManager();
EditNameDialog editNameDialog = new EditNameDialog();
editNameDialog.show(ft, "fragment_edit_name");
}
@Override
public void onFinishEditDialog(String inputText) {
Toast.makeText(getActivity(), "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
}
最佳答案
用它来显示 fragment 中的dialogfragment
FragmentManager fm = getActivity().getSupportFragmentManager();
DialogFragment dialog = new MyDialogFragment(); // creating new object
dialog.show(fm, "dialog");
关于android - 如何在ActionBar Android中显示来自Fragment的DialogFragment,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15459002/