Fragment的Android对话框

时间:2016-03-30 21:03:36

标签: java android xml android-fragments android-studio

我尝试通过单击Fragment中的LinearLayout打开对话框。其实我按照这个教程: https://github.com/codepath/android_guides/wiki/Using-DialogFragment 它有效,但Activity(oncreate)中有对话框。如果我试图把它放在片段中它就不起作用。

Dialog.java是:

package com.hgyghyfghyu.apkana40;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
// ...

public class EditNameDialog extends DialogFragment {

    private EditText mEditText;

    public EditNameDialog() {
    }

    public static EditNameDialog newInstance(String title) {
        EditNameDialog frag = new EditNameDialog();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_edit_name, container);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mEditText = (EditText) view.findViewById(R.id.txt_your_name);
        String title = getArguments().getString("title", "Enter Name");
        getDialog().setTitle(title);
        mEditText.requestFocus();
        getDialog().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }
}

和Fragment.java是:

package com.hgyghyfghyu.apkana40;


import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
//import android.support.v4.app.FragmentManager;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import static com.hgyghyfghyu.apkana40.AppData.FCOLOR_KEY;
import static com.hgyghyfghyu.apkana40.AppData.TRANSP_KEY;


/**
 * A simple {@link Fragment} subclass.
 */
public class TrainerMenu extends Fragment {


    public TrainerMenu() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        SharedPreferences pref = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);

        View v = inflater.inflate(R.layout.fragment_trainer_menu, container, false);
        v.findViewById(R.id.trainermenuadbtn).setBackgroundColor(Color.parseColor(pref.getString(TRANSP_KEY, "#CC") + pref.getString(FCOLOR_KEY, "33777F")));
        v.findViewById(R.id.trainermenutrainersbtn).setBackgroundColor(Color.parseColor(pref.getString(TRANSP_KEY, "#CC") + pref.getString(FCOLOR_KEY, "33777F")));
        v.findViewById(R.id.trainermenusettingsbtn).setBackgroundColor(Color.parseColor(pref.getString(TRANSP_KEY, "#CC") + pref.getString(FCOLOR_KEY, "5556FF")));
        v.findViewById(R.id.trainermenuinfobtn).setBackgroundColor(Color.parseColor(pref.getString(TRANSP_KEY, "#CC") + pref.getString(FCOLOR_KEY, "5556FF")));
        v.findViewById(R.id.trainermenugroupsbtn).setBackgroundColor(Color.parseColor(pref.getString(TRANSP_KEY, "#CC") + pref.getString(FCOLOR_KEY, "5556FF")));


        return v;
    }

    public void onViewCreated (View view, Bundle savedInstanceState) {
        LinearLayout btn = (LinearLayout) view.findViewById(R.id.pomocniczy);
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //DialogFragment dialog = new DialogFragment(getActivity());
                showEditDialog();
                return true;
            }
        });

    }
    private void showEditDialog() {
        android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
        EditNameDialog editNameDialog = EditNameDialog.newInstance("Some Title");
        editNameDialog.show(fm, "fragment_edit_name");
    }

}

有错误

Error:(63, 53) error: cannot find symbol method getSupportFragmentManager()

我写的那个问题是因为我使用extend Fragment而不是FragmentActivity但是当检查我的片段扩展时它不起作用:

SharedPreferences pref = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);

从片段打开对话框的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

使用它:

android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();

访问活动上下文