gpt4 book ai didi

java - 来自 Fragment 的 Android 对话框

转载 作者:行者123 更新时间:2023-12-01 10:08:01 27 4
gpt4 key购买 nike

我尝试通过单击 Fragment 中的 LinearLayout 打开对话框。实际上我遵循这个教程: https://github.com/codepath/android_guides/wiki/Using-DialogFragment它可以工作,但 Activity (oncreate)中有dialogshow。如果我尝试将其放入 fragment 中,则不起作用。

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()

我写道这个问题是因为我使用扩展 Fragment 而不是 FragmentActivity 但当检查我的 fragment 扩展时它不起作用:

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

从 fragment 打开对话框的最佳方式是什么?

最佳答案

使用这个:

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

访问 Activity 上下文

关于java - 来自 Fragment 的 Android 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36319696/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com