本文介绍了Android的按钮setAlpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有一组按钮,我想要得到的结果是:当我点击其中的一个,首先我将它们分为两部分:点击一个和其他人。我想设置不同的颜色或Alpha值不同的它们。现在我用 setAlpha ,但是当我从更改值 0〜255 ,它的工作原理,但是当我改变的值 255至0 ,它亘古不变的工作。我不知道为什么。也许以后我调用该方法 Button.setAlpha(),我需要调用其他方法?我的code: 公共类MainActivity延伸活动{ //按钮alpha值:减少值 公共静态INT BUTTON_ALPHA_MIN = 0; //按钮alpha值:实现价值最大化 公共静态INT BUTTON_ALPHA_MAX = 255; 私人的LinearLayout centerRegion; 私人的LinearLayout bottomRegion; 私人按钮btnCheckIn; 私人按钮btnReview; 私人按钮btnMyCircles; 私人按钮btnSettings; @覆盖 公共无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); //获取所有的部件 getAllWidgets(); //设置按钮点击响应函数 btnCheckIn.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ centerRegion.setBackgroundColor(android.graphics.Color.RED); 。btnReview.getBackground()setAlpha(BUTTON_ALPHA_MIN); 。btnMyCircles.getBackground()setAlpha(BUTTON_ALPHA_MIN); 。btnSettings.getBackground()setAlpha(BUTTON_ALPHA_MIN); 。btnCheckIn.getBackground()setAlpha(BUTTON_ALPHA_MAX); } }); btnReview.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ centerRegion.setBackgroundColor(android.graphics.Color.BLUE); 。btnCheckIn.getBackground()setAlpha(BUTTON_ALPHA_MIN); 。btnMyCircles.getBackground()setAlpha(BUTTON_ALPHA_MIN); 。btnSettings.getBackground()setAlpha(BUTTON_ALPHA_MIN); 。btnReview.getBackground()setAlpha(BUTTON_ALPHA_MAX); } }); btnMyCircles.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ centerRegion.setBackgroundColor(android.graphics.Color.YELLOW); 。btnCheckIn.getBackground()setAlpha(BUTTON_ALPHA_MAX); 。btnReview.getBackground()setAlpha(BUTTON_ALPHA_MAX); 。btnSettings.getBackground()setAlpha(BUTTON_ALPHA_MAX); 。v.getBackground()setAlpha(BUTTON_ALPHA_MIN); } }); btnSettings.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ centerRegion.setBackgroundColor(android.graphics.Color.MAGENTA); 。btnCheckIn.getBackground()setAlpha(BUTTON_ALPHA_MAX); 。btnReview.getBackground()setAlpha(BUTTON_ALPHA_MAX); 。btnMyCircles.getBackground()setAlpha(BUTTON_ALPHA_MAX); 。v.getBackground()setAlpha(BUTTON_ALPHA_MIN); } }); } / ** *让所有的部件 * / 公共无效getAllWidgets(){ this.centerRegion =(的LinearLayout)this.findViewById(R.id.center_region); this.bottomRegion =(的LinearLayout)this.findViewById(R.id.bottom_region); this.btnCheckIn =(按钮)this.findViewById(R.id.button_check_in); this.btnReview =(按钮)this.findViewById(R.id.button_review); this.btnMyCircles =(按钮)this.findViewById(R.id.button_my_circles); this.btnSettings =(按钮)this.findViewById(R.id.button_setting); }} 解决方案 使用AlphaAnimation应该工作;验证我的设备上。 公共类测试扩展活动实现OnClickListener { 私人AlphaAnimation alphaDown; 私人AlphaAnimation alphaUp; 私人按钮B1; 私人按钮B2; 公共无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); 的LinearLayout LL =(的LinearLayout)findViewById(R.id.linear_layout); B1 =新的按钮(这一点); b1.setText(按钮1); b1.setOnClickListener(本); ll.addView(B1); B2 =新的按钮(这一点); b2.setText(按钮2); b2.setOnClickListener(本); ll.addView(B2); alphaDown =新AlphaAnimation(1.0F,0.3f); alphaUp =新AlphaAnimation(0.3f,1.0F); alphaDown.setDuration(1000); alphaUp.setDuration(1000); alphaDown.setFillAfter(真正的); alphaUp.setFillAfter(真正的); } 公共无效的onClick(视图v){ 如果(V == B1){ b1.startAnimation(alphaUp); b2.startAnimation(alphaDown); } 其他 { b1.startAnimation(alphaDown); b2.startAnimation(alphaUp); } }} 关键是呼吁 setFillAfter(真)使得α变化仍然存在。There are a set of buttons, I want to get the result:When I click one of them, first I divide them into two parts: the clicked one and the others. I'm trying to set different color or alpha value to different them.Now I use setAlpha, but when I change the value from 0 to 255, it works, but when I change the value from 255 to 0 , it doesnot work. I don't know why.Maybe after I invoke the methodButton.setAlpha(), I need invoke another method?my code:public class MainActivity extends Activity { // button alpha value: minimize value public static int BUTTON_ALPHA_MIN = 0; // button alpha value: maximize value public static int BUTTON_ALPHA_MAX = 255; private LinearLayout centerRegion; private LinearLayout bottomRegion; private Button btnCheckIn; private Button btnReview; private Button btnMyCircles; private Button btnSettings; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // get all the widgets getAllWidgets(); // set buttons click response function btnCheckIn.setOnClickListener(new OnClickListener() { public void onClick(View v) { centerRegion.setBackgroundColor(android.graphics.Color.RED); btnReview.getBackground().setAlpha(BUTTON_ALPHA_MIN); btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN); btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN); btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX); } }); btnReview.setOnClickListener(new OnClickListener() { public void onClick(View v) { centerRegion.setBackgroundColor(android.graphics.Color.BLUE); btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MIN); btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN); btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN); btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX); } }); btnMyCircles.setOnClickListener(new OnClickListener() { public void onClick(View v) { centerRegion.setBackgroundColor(android.graphics.Color.YELLOW); btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX); btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX); btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MAX); v.getBackground().setAlpha(BUTTON_ALPHA_MIN); } }); btnSettings.setOnClickListener(new OnClickListener() { public void onClick(View v) { centerRegion.setBackgroundColor(android.graphics.Color.MAGENTA); btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX); btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX); btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MAX); v.getBackground().setAlpha(BUTTON_ALPHA_MIN); } }); } /** * get all the widgets */ public void getAllWidgets() { this.centerRegion = (LinearLayout) this.findViewById(R.id.center_region); this.bottomRegion = (LinearLayout) this.findViewById(R.id.bottom_region); this.btnCheckIn = (Button) this.findViewById(R.id.button_check_in); this.btnReview = (Button) this.findViewById(R.id.button_review); this.btnMyCircles = (Button) this.findViewById(R.id.button_my_circles); this.btnSettings = (Button) this.findViewById(R.id.button_setting); }} 解决方案 Using AlphaAnimation should work; verified on my device.public class Test extends Activity implements OnClickListener { private AlphaAnimation alphaDown; private AlphaAnimation alphaUp; private Button b1; private Button b2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout ll = (LinearLayout) findViewById(R.id.linear_layout); b1 = new Button(this); b1.setText("Button 1"); b1.setOnClickListener(this); ll.addView(b1); b2 = new Button(this); b2.setText("Button 2"); b2.setOnClickListener(this); ll.addView(b2); alphaDown = new AlphaAnimation(1.0f, 0.3f); alphaUp = new AlphaAnimation(0.3f, 1.0f); alphaDown.setDuration(1000); alphaUp.setDuration(1000); alphaDown.setFillAfter(true); alphaUp.setFillAfter(true); } public void onClick(View v) { if (v == b1) { b1.startAnimation(alphaUp); b2.startAnimation(alphaDown); } else { b1.startAnimation(alphaDown); b2.startAnimation(alphaUp); } }}The key is calling setFillAfter(true) so that the alpha change persists. 这篇关于Android的按钮setAlpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 10:59