2017-04-20 57 views
0

我正在构建一个应用程序,并希望在弹出窗口中更改团队颜色。我使用ImageButtons向用户显示团队颜色。主要活动中的一个按钮和弹出窗口中的四个按钮。当我在弹出窗口中单击一个时,我可以切换背景,但当我关闭弹出窗口并再次打开时,弹出窗口中的按钮已被复位。在弹出窗口中更改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

如果我正确理解你的答案,你想在点击时“保存”按钮背景颜色。如果是这样,您可以使用共享首选项(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(...) 

我希望这有助于:)