Java 如果按钮被单击或未被剪辑,则生成按钮方法
在这里,我有很多按钮,随机变成可见Java 如果按钮被单击或未被剪辑,则生成按钮方法,java,android,button,Java,Android,Button,在这里,我有很多按钮,随机变成可见 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
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 );
}
但我想做一个方法,如果按钮“不点击”,当它是可见的,所以当按钮不点击,他会做一些代码
我的意思是像这样
//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
}
}
有人能教我如何使用一些代码吗?
注:
对不起,我忘了写部分代码,它留在我的电脑上了
所以我可以举这样的例子:
每1秒按钮被随机设置为可见,因此每1秒按钮被随机设置为可见,并且1秒之前可见的按钮将不可见您可以定义一个布尔值并为按钮使用onclicklistener,然后如果用户单击按钮,布尔值将为false,他无法编写代码。这是一面旗帜 您可以定义一个布尔值,并为按钮使用onclicklistener,然后如果用户单击按钮,布尔值将为false,他无法编写代码。这是一面旗帜
b1.setOnClickListener(新视图.OnClickListener()
b1.setOnClickListener(new View.OnClickListener()
{
Button[] s=new Button[]{bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8,
bt9, bt10, bt11, bt12, bt13, bt14, bt15, bt16};
Random generator = new Random();
int number = generator.nextInt(16);
@Override
public void onClick(View v) {
for( int i=0; i<s.length; i++ )
{
if( i == number )
s[i].setVisibility(View.VISIBLE);
else
s[i].setVisibility(View.INVISIBLE);
}
}
});
{
按钮[]s=新按钮[]{bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,
bt9、bt10、bt11、bt12、bt13、bt14、bt15、bt16};
随机生成器=新随机();
int编号=发电机。nextInt(16);
@凌驾
公共void onClick(视图v){
对于(inti=0;ib1.setOnClickListener(newview.OnClickListener())
{
按钮[]s=新按钮[]{bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,
bt9、bt10、bt11、bt12、bt13、bt14、bt15、bt16};
随机生成器=新随机();
int编号=发电机。nextInt(16);
@凌驾
公共void onClick(视图v){
对于(inti=0;i检查此
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);
看看这个
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);
你的意思是说,如果一段时间后没有点击可见按钮,你想做点什么,对吗?是的,关键是当按钮可见而没有点击时,会做些什么@G2O不,我想他是说当点击一个按钮时,没有点击的按钮会发生一些事情。但是我想做一个方法,如果按钮n“不点击”当按钮可见时,当按钮未点击时,他将执行一些代码。
意味着什么?如果按钮可见一段时间而用户未按下按钮?对不起,我的错误,请查看我的解释我更新了我的任务@NJ nilesh你的意思是,如果一段时间后未点击可见按钮,你想做点什么,对吗,关键是当按钮可见且未单击时,那么将执行某些操作@G2O不,我认为他的意思是,当单击一个按钮时,未单击的按钮应发生某些操作。但如果按钮“未单击”,我想创建一个方法当它可见时,当按钮未点击时,他将执行一些代码。
意味着什么?如果按钮可见一段时间而用户未按下按钮?对不起,我的错误,请查看我的解释我更新了我的任务@NJ nilesh您的代码是否编译?如果是,这只能用于随机设置可见性一次。您的代码是否ven compile?如果是,则只能使用一次来随机设置可见性。
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;
}
}