制作方法按钮,如果单击按钮或不cliked [英] Make button method if button clicked or not cliked
问题描述
在这里我有很多按钮,它随机变成可见
BT1 =(按钮)findViewById(R.id.yellow1);
BT2 =(按钮)findViewById(R.id.yellow2);
BT3 =(按钮)findViewById(R.id.yellow3);
BT4 =(按钮)findViewById(R.id.yellow4);
BT5 =(按钮)findViewById(R.id.yellow5);
BT6 =(按钮)findViewById(R.id.yellow6);
BT7 =(按钮)findViewById(R.id.yellow7);
BT8 =(按钮)findViewById(R.id.yellow8);
BT9 =(按钮)findViewById(R.id.yellow9);
BT10 =(按钮)findViewById(R.id.yellow10);
BT11 =(按钮)findViewById(R.id.yellow11);
BT12 =(按钮)findViewById(R.id.yellow12);
BT13 =(按钮)findViewById(R.id.yellow13);
BT14 =(按钮)findViewById(R.id.yellow14);
BT15 =(按钮)findViewById(R.id.yellow15);
BT16 =(按钮)findViewById(R.id.yellow16); 按钮[]按钮=新的Button [] {BT1,BT2,BT3,BT4,BT5,BT6,BT7,BT8,
BT9,BT10,BT11,BT12,BT13,BT14,BT15,BT16}; 随机数发生器=新的随机();
数= generator.nextInt(16); 的for(int i = 0; I< buttons.length;我++)
{
如果(我==号)
按钮[I] .setVisibility(View.VISIBLE);
其他
按钮[I] .setVisibility(View.INVISIBLE);
}
按钮是随机可见,如果轮到可见另一个是不可见的。当然还有一个方法,如果按键处于点击到可见的按钮
如果(点击== || BT1点击== || BT2点击== || BT3点击== || BT4点击== || BT5点击== || BT6点击== || BT7点击== || BT8
点击== || BT9点击== || BT10点击== || BT11点击== || BT12点击== || BT13点击== || BT14点击== || BT15点击== BT16){ //会做一些 }
}
但我想打一个方法,如果按钮没有点击时,它是可见的,所以当没有按下按钮,他会做一些code。
我的意思是这样的。
//只是例子
如果没有按钮点击(点击== || BT1点击== || BT2点击== || BT3点击== || BT4点击== || BT5点击== || BT6点击== || BT7点击== BT8 ||
点击== || BT9点击== || BT10点击== || BT11点击== || BT12点击== || BT13点击== || BT14点击== || BT15点击== BT16){ //所以做什么 }
}
谁能教我怎么做,有些code?
请注意:
块引用>对不起,我忘了写了code的某一部分,它留给我的电脑!
所以我只可以给的例子是这样的:
每1秒按钮随机设置为可见的,所以每1秒就有随机按钮设置为可见和按钮可见1秒之前是不可见的。
解决方案看看这个
处理程序visibilityToggler =新的处理程序();
可运行visivilityRunnable =新的Runnable(){
@覆盖
公共无效的run(){ // isUserClickedButton是用来保持记录,如果用户在1秒内pressed按钮
//保持isUserClickedButton =真正的第一次,因为它会运行
如果(!isUserClickedButton){ //用户没有pressed按钮
Toast.makeText(背景下,你是不是pressed按钮,Toast.LENGHT_LONG).show();
} //切换知名度
随机数发生器=新的随机();
数= generator.nextInt(16); 的for(int i = 0; I< buttons.length;我++){
如果(我==号)
按钮[I] .setVisibility(View.VISIBLE);
其他
按钮[I] .setVisibility(View.INVISIBLE);
} //再次启动能见度
visibilityToggler.postDelayed(visivilityRunnable,1000); //使假能见度被触发,我们要跟踪从开始pssed按钮$ P $
isUserClickedButton = FALSE;
}
};visibilityToggler.postDelayed(visivilityRunnable,1000);
的onclick
处理,如果用户pressed按钮
块引用>如果(点击== || BT1点击== || BT2点击== || BT3点击== || BT4点击== || BT5点击== || BT6点击== || BT7点击== || BT8
点击== || BT9点击== || BT10点击== || BT11点击== || BT12点击== || BT13点击== || BT14点击== || BT15点击== BT16){ //会做一些
//为用户pressed按钮使它真正的,我们不希望运行1秒后pssed没有$ P $的条件
isUserClickedButton = TRUE;
}
}in here i had a lot of button that randomly turn to visible
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 ); }
button is randomly visible, if one turn to visible another one will be invisible. and of course a method if button was "click" to that visible 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 } }
but i want to make a method if button "not click" when it is visible, so when button not clicked he will do some code.
i mean is like this
//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 } }
Can anyone teach me how to do that with some code?
NOTE:
Sorry i forget to write some part of the code, it is left on my computer!
So i just can give example like this:
Every 1second the button is randomly set to visible, so every 1second there is randomly button set to visible and the button that visible 1second before will be invisible
解决方案Check out this
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; } }
这篇关于制作方法按钮,如果单击按钮或不cliked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!