2017-04-20 5 views
0

私はアプリケーションを構築していて、ポップアップウィンドウでチームカラーを変更したいと考えています。私はImageButtonsを使用して、ユーザーにチームカラーを示しました。メインアクティビティの1つのボタンと、ポップアップウィンドウの4つのボタン。ポップアップウィンドウでクリックすると背景を切り替えることができますが、ポップアップウィンドウを閉じてもう一度開くと、ポップアップウィンドウのボタンがリセットされます。ポップアップウィンドウでImageButtonの背景を変更する

リセットせずにポップアップウィンドウを閉じるにはどうしたらいいですか?

public void colorchange(final View view){ 

    layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
    ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.popwindow, null); 
    relativeLayout = (RelativeLayout) findViewById(R.id.popup); 
    popupWindow = new PopupWindow(container, relativeLayout.getWidth(), relativeLayout.getHeight(), true); 
    popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, (int)relativeLayout.getX(),(int)relativeLayout.getY()); 
    ImageButton narancs = (ImageButton) container.findViewById(R.id.imgbutton1); 

    container.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      popupWindow.dismiss(); 
      return false; 
     } 
    }); 

    narancs.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Drawable asds = v.getBackground(); 
      Log.d("hatter", String.valueOf(asds)); 
      v.setBackgroundResource(R.drawable.bluebutton); 
      view.setBackgroundResource(R.drawable.orangebutton); 
      Log.d("hatter", String.valueOf(view.getBackground())); 
      //popupWindow.dismiss(); 
     } 
    }); 

} 

このコードは、mainactivityボタンのクリックイベントで実行されます。

答えて

0

あなたの答えを正しく理解していれば、クリックするとボタンの背景色を「保存」したいと思っています。そうであれば、Shared Preferences(Android Developer - Shared Preferences)を使用して、選択した色をクリックして保存することができます。アクティビティでは、共有された環境設定からボタンの色を設定できます。

SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0); 
SharedPreferences.Editor editor = settings.edit(); 
editor.putString(key, value); 
editor.apply(); 

その後クリック時

、あなたが背景の色を設定するには、この値をロードすることができ

SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0); 
//You can use a switch or if else 
if(settings.getString(key, defValue).toUppercase.equals("BLUE")){ 
//set Color Blue as Background 
v.setBackgroundResource(R.drawable.bluebutton); 
}else if(...) 

私はこれが役に立てば幸い:)

関連する問題