我有一个按钮和两个图像,我希望按钮的默认图像为 btn1.jpg ,单击该按钮时,图像应立即更改为 btn2.jpg , 3秒后,它应该再次恢复为 btn1.jpg 。请告诉我如何实现这一目标?
package com.example.btn;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
private View ButtonName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void yolo(View v) {
switch (v.getId()) {
case R.id.buttonName:
ButtonName.setBackgroundResource(R.drawable.btn2);
//Disable click on Button
ButtonName.setEnabled(false);
try {
Thread.sleep(3000);
}
catch (Exception e) {
e.printStackTrace();
}
ButtonName.setBackground(getResources().getDrawable(R.drawable.btn1));
break;
case default:
ButtonName.setBackgroundResource(R.drawable.btn1);
}
}
}
答案 0 :(得分:1)
您必须将OnClick方法中的按钮背景图像更改为 btn2.jpg 。之后,您必须启动计时器倒计时3秒,然后再将按钮图像再次更改为 btn1.jpg
private final int interval = 3000;
private Handler handler = new Handler();
private Runnable runnable
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
btn.setBackground(getResources().getDrawable(R.drawable.btn2))
//Start runnable after 3 seconds
handler.postDelayed(runnable, interval);
}
});
runnable = new Runnable(){
public void run() {
btn.setBackground(getResources().getDrawable(R.drawable.btn1))
}
};
答案 1 :(得分:1)
终于弄清楚了自己!
在xml中设置按钮的背景
使用此代码:
package com.example.btn;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
public class MainActivity extends Activity {
Handler mHandler; // global instance
Runnable your_runnable; // global instance
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void yolo(final View view) {
if (view == view) {
view.setBackgroundResource(R.drawable.btn1);
mHandler = new Handler();
your_runnable = new Runnable() {
@Override
public void run() {
view.setBackgroundResource(R.drawable.btn2);
}
};
mHandler.postDelayed(your_runnable, 3000L);// 3sec timer
}
}
}
答案 2 :(得分:0)
好的,首先,你在这里犯了一个错误:
ButtonName.setBackgroundResource(getresources().getDrawable(R.drawable.btn2));
之后,在按钮上添加clickListener:
private Thread t = new Thread(new Runnable {
@Override
public void run() {
ButtonName.setBackgroundResource(getresources().getDrawable(R.drawable.btn2));
//Disable click on Button
ButtonName.setEnabled(false);
try {
Thread.sleep(3000);
}
catch (Exception e) {
e.printstacktrace();
}
ButtonName.setBackgroundResource(getresources().getDrawable(R.drawable.btn1));
}
});
ButtonName.setOnClickListener (new OnClickListener (
@Override
public void onClick(View v) {
t.start();
}
));
我认为这就是你想要的
答案 3 :(得分:0)
这可能对你有用!!
public class MainActivity extends Activity {
Button button;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.yourbuttonid);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
button.setBackgroundResource(getresources().getDrawable(R.drawable.btn1));
handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
button.setBackgroundResource(getresources().getDrawable(R.drawable.btn2));
}
}, 3000);
}
});
}