在这里我有很多随机变成可见的按钮
bt1 = (Button)findViewById(R.id.yellow1);
bt2 = (Button)findViewById(R.id.yellow2);
bt3 = (Button)findViewById(R.id.yellow3);
bt4 = (Button)findViewById(R.id.yellow4);
bt5 = (Button)findViewById(R.id.yellow5);
bt6 = (Button)findViewById(R.id.yellow6);
bt7 = (Button)findViewById(R.id.yellow7);
bt8 = (Button)findViewById(R.id.yellow8);
bt9 = (Button)findViewById(R.id.yellow9);
bt10 = (Button)findViewById(R.id.yellow10);
bt11 = (Button)findViewById(R.id.yellow11);
bt12 = (Button)findViewById(R.id.yellow12);
bt13 = (Button)findViewById(R.id.yellow13);
bt14 = (Button)findViewById(R.id.yellow14);
bt15 = (Button)findViewById(R.id.yellow15);
bt16 = (Button)findViewById(R.id.yellow16);
Button[] buttons = new Button[]{ bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8,
bt9, bt10, bt11, bt12, bt13, bt14, bt15, bt16 };
Random generator = new Random();
number = generator.nextInt(16);
for( int i=0; i<buttons.length; i++ )
{
if( i == number )
buttons[i].setVisibility( View.VISIBLE );
else
buttons[i].setVisibility( View.INVISIBLE );
}
按钮是随机可见的,如果一个变为可见,另一个将不可见。如果按钮被“点击”到那个可见按钮,当然还有一个方法
if(click==bt1|| click==bt2|| click==bt3|| click==bt4 || click==bt5|| click==bt6|| click==bt7 || click==bt8||
click==bt9|| click==bt10 || click==bt11|| click==bt12|| click==bt13 || click==bt14|| click==bt15|| click==bt16){
//will do something
}
}
但我想创建一个方法,如果按钮在可见时“未单击”,那么当按钮未单击时,他将执行一些代码。
我的意思是这样
//just example
if button not clicked(click==bt1|| click==bt2|| click==bt3|| click==bt4 || click==bt5|| click==bt6|| click==bt7 || click==bt8||
click==bt9|| click==bt10 || click==bt11|| click==bt12|| click==bt13 || click==bt14|| click==bt15|| click==bt16){
//so do something
}
}
谁能教我如何用一些代码做到这一点?
NOTE:
对不起,我忘了写部分代码,它留在我的电脑上!
所以我可以举个例子:
每 1 秒按钮随机设置为可见,因此每 1 秒有一个随机按钮设置为可见,1 秒前可见的按钮将不可见
最佳答案
看看这个
Handler visibilityToggler = new Handler();
Runnable visivilityRunnable = new Runnable() {
@Override
public void run() {
// isUserClickedButton is used to keep record if user has pressed button within 1 sec
// keep isUserClickedButton = true for first time as it will run
if (!isUserClickedButton) {
// user not pressed button
Toast.makeText(context,"You are not pressed the Button",Toast.LENGHT_LONG).show();
}
// toggle visibility
Random generator = new Random();
number = generator.nextInt(16);
for (int i = 0; i < buttons.length; i++) {
if (i == number)
buttons[i].setVisibility(View.VISIBLE);
else
buttons[i].setVisibility(View.INVISIBLE);
}
// again start the visibility
visibilityToggler.postDelayed(visivilityRunnable,1000);
// make it false as visibility is toggled and we want to track button pressed from start
isUserClickedButton = false;
}
};
visibilityToggler.postDelayed(visivilityRunnable,1000);
Onclick
handling if user pressed button
if (click == bt1 || click == bt2 || click == bt3 || click == bt4 || click == bt5 || click == bt6 || click == bt7 || click == bt8 ||
click == bt9 || click == bt10 || click == bt11 || click == bt12 || click == bt13 || click == bt14 || click == bt15 || click == bt16) {
//will do something
// make it true as user is pressed button and we don't want to run condition of not pressed after 1 sec
isUserClickedButton = true;
}
}
关于java - 如果单击或未单击按钮,则制作按钮方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35388308/