繁体   English   中英

如何将按钮背景更改为图像,然后在按下时恢复为默认按钮

[英]How to change a button background to image then back to default button when pressed

嗨,我是该网站的新手,但潜伏了一段时间。 编程新手。

我的问题是我想按一个按钮,然后该按钮显示图像一定时间,然后返回其原始状态。 到目前为止,我可以将按钮更改/转换为图像,但是它不会恢复为按钮状态。 或者,我可以使其来回过渡但没有图像。

忽略其他按钮,此刻仅对Button1尝试一下。 提前致谢。

这是我的代码:

public class MainActivity extends Activity implements OnClickListener {

    private Button Button1, Button2, Button3, Button4,
                   Button5, Button6, Button7, Button8,
                   Button9, Button10, Button11, Button12;

    private Button ButtonNewGame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Declaring Buttons/Squares
        Button1 = (Button) findViewById(R.id.Button1);
        Button2 = (Button) findViewById(R.id.Button2);
        Button3 = (Button) findViewById(R.id.Button3);
        Button4 = (Button) findViewById(R.id.Button4);
        Button5 = (Button) findViewById(R.id.Button5);
        Button6 = (Button) findViewById(R.id.Button6);
        Button7 = (Button) findViewById(R.id.Button7);
        Button8 = (Button) findViewById(R.id.Button8);
        Button9 = (Button) findViewById(R.id.Button9);
        Button10 = (Button) findViewById(R.id.Button10);
        Button11= (Button) findViewById(R.id.Button11);
        Button12 = (Button) findViewById(R.id.Button12);
        //Declaring NewGame Button
        ButtonNewGame = (Button) findViewById(R.id.ButtonNewGame);

        //Adding OnClickListeners
        Button1.setOnClickListener(this);
        Button2.setOnClickListener(this);
        Button3.setOnClickListener(this);
        Button4.setOnClickListener(this);
        Button5.setOnClickListener(this);
        Button6.setOnClickListener(this);
        Button7.setOnClickListener(this);
        Button8.setOnClickListener(this);
        Button9.setOnClickListener(this);
        Button10.setOnClickListener(this);
        Button11.setOnClickListener(this);
        Button12.setOnClickListener(this);

        ButtonNewGame.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //Button1.setBackgroundResource(R.drawable.ic_launcher);
        Animation animation = new AlphaAnimation(1, 0);
        animation.setDuration(1000);
        animation.setInterpolator( new LinearInterpolator());
        animation.setRepeatCount(Animation.ABSOLUTE);
        animation.setRepeatMode(Animation.REVERSE);

        if(v == Button1){
            Button1.startAnimation(animation);
        }
    }
}

尝试StateListDrawable

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/a" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/b" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/c" /> <!-- hovered -->
    <item android:drawable="@drawable/d" /> <!-- default -->
</selector>

这可能是多余的,但是您可以使用内部的CountDownTimer ,该Button在您单击Button时启动。 更改“ Button图像时也是如此。 然后,在CountDownTimers onFinish()中 ,将Button的背景设置为默认值

这个作品我用过了

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_focused="true" android:state_pressed="true" 
        android:drawable="@drawable/ok_button_hover" /> 
  <item android:state_focused="false" android:state_pressed="true" 
        android:drawable="@drawable/ok_button_hover" /> 
  <item android:drawable="@drawable/ok_button" /> 
</selector>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM