我想使用延迟但内部有条件。任何类似的东西。
这个方法是延迟,当时间结束时我调用方法responde(),但setText不起作用。
public void pensar(View v){
respuesta.setText("Ummmm");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
responde();
}
});
}
}, 1000);
}
Responde()方法:
public void responde() {
if (sino == 1) {
respuesta.setText(R.string.yes);
} else if (sino == 2) {
respuesta.setText(R.string.no);
} else {
respuesta.setText(R.string.dontknow);
}
sino = 0;
}
应用程序没有崩溃,只是不起作用。
答案 0 :(得分:0)
这是我的主要课程:
public class MainActivity extends AppCompatActivity {
private Button pregunta;
private TextView respuesta;
int menuCode = 0;
boolean respuestasPersonalizas = false;
int sino = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
pregunta = (Button)findViewById(R.id.pregunta);
respuesta = (TextView)findViewById(R.id.respuesta);
}
public void responde() {
if (respuestasPersonalizas = false) {
if (sino == 1) {
respuesta.setText(R.string.yes);
} else if (sino == 2) {
respuesta.setText(R.string.no);
} else {
respuesta.setText(R.string.dontknow);
}
sino = 0;
}
}
public void pensar(View v){
respuesta.setText("Ummmm");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
responde();
}
});
}
}, 1000);
}
//LOGICA
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
sino = 1;
}else if(menuCode == 0){
menuCode = 1;
}else if( menuCode == 1){
menuCode = 2;
}else{
menuCode=0;
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
sino = 2;
}else if(menuCode == 2){
menuCode = 3;
}else{
menuCode = 0;
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
//ACCEDER AL MENÚ
@Override
public boolean onTouchEvent(MotionEvent e) {
accederMenu();
return true;
}
public void accederMenu(){
Intent objIntent;
if(menuCode == 3){
objIntent = new Intent(this, menu.class);
startActivity(objIntent);
menuCode = 0;
}else{
menuCode = 0;
}
}
}