Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将按钮背景更改为图像,然后在按下时返回默认按钮_Java_Android_Button_Background_Onclick - Fatal编程技术网

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

Java 如何将按钮背景更改为图像,然后在按下时返回默认按钮,java,android,button,background,onclick,Java,Android,Button,Background,Onclick,嗨,我是这个网站的新手,但已经潜伏了一段时间。编程新手 我的问题是,我想按下一个按钮,然后该按钮在一定时间内显示图像,然后返回到其原始状态。到目前为止,我可以将按钮更改/转换为图像,但它不会恢复为其按钮。或者我可以在没有图像的情况下来回转换 忽略其他按钮,只是在此时尝试按钮1。提前谢谢 这是我的密码: public class MainActivity extends Activity implements OnClickListener { private Button Button

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

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

忽略其他按钮,只是在此时尝试按钮1。提前谢谢

这是我的密码:

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);
        }
    }
}
尝试一种状态列表


这可能有点过头了,但你可以使用一个内部按钮,当你点击按钮时启动。这也是更改按钮图像时出现的情况。然后,在倒计时中,您将按钮的背景设置为默认值

我已经使用过了

<?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>

使用按钮1.setBackgroundResourceR.drawable.ic_发射器;注释掉它进行转换并恢复到原始状态。没有注释,它会显示图像,但不会恢复。我不认为这会起作用,因为我只想在按下按钮后显示几秒钟的图像。几秒钟后,我希望按钮恢复默认状态。太好了,非常感谢。反应也很快不客气!如果这解决了您的问题,请接受,这样可以更容易地帮助他人
<?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>