2016-04-03 217 views
0

我想通過單擊LinearLayout(id:colorsdialogbtn)從Fragment(TrainerMenu)打開對話框(SettingsFColorDialog),但實際上通過一次觸摸打開兩個對話框(相同)。我不知道,因爲我使用 「showEditDialog」 只是一個時間什麼是錯的...Android打開對話框

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

您需要使用setOnClickListner才能聽到點擊數量不是setOnTouchListener,觸摸時可以觸發很多點擊。