我想通过点击LinearLayout(id:colorsdialogbtn)从Fragment(TrainerMenu)打开对话框(SettingsFColorDialog),但实际上只需轻轻一按即可打开两个Dialogs(相同)。我不知道出了什么问题,因为我使用" showEditDialog"只有一次......
SettingsFColorDialog:
package com.hgyghyfghyu.apkana40;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import static com.hgyghyfghyu.apkana40.AppData.FCOLOR_KEY;
import static com.hgyghyfghyu.apkana40.AppData.editordata;
import static com.hgyghyfghyu.apkana40.AppData.prefdata;
// ...
public class SettingsFColorDialod extends DialogFragment {
public SettingsFColorDialod() {
}
public static SettingsFColorDialod newInstance(String title) {
SettingsFColorDialod frag = new SettingsFColorDialod();
Bundle args = new Bundle();
args.putString("Choose your color", title);
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.settingsfcolordialog, container);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LinearLayout firstcolor = (LinearLayout) view.findViewById(R.id.FIRSTCOLOR);
LinearLayout secondcolor = (LinearLayout) view.findViewById(R.id.SECONDCOLOR);
prefdata = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
editordata = prefdata.edit();
firstcolor.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editordata.putString(FCOLOR_KEY,"33777F").apply();
dismiss();
return true;
}
});
secondcolor.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
String title = getArguments().getString("Choose your color", "Choose your color");
getDialog().setTitle(title);
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
}
TrainerMenu:
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.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;
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.colorsdialogbtn);
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
showEditDialog();
return true;
}
});
}
private void showEditDialog() {
android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
SettingsFColorDialod settingsFColorDialod = SettingsFColorDialod.newInstance("Some Title");
settingsFColorDialod.show(fm, "SettingsFColorDialog");
}
}
有什么不对吗?
答案 0 :(得分:0)
您需要使用setOnClickListner
来监听不是setOnTouchListener
的点击,这些点击可以在触摸时大量触发。