如果点击按钮或未点击按钮,则按下按钮方法

时间:2016-02-14 04:18:08

标签: 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);
     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 );
            }

按钮随机可见,如果一个转向可见,则另一个按钮将不可见。当然,如果按钮是&#34;点击&#34;到那个可见的按钮

    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

               }        
            }   

但我想制作一个方法,如果按钮&#34;不点击&#34;当它可见时,所以当没有点击按钮时,他会做一些代码。

我的意思是这样的

    //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秒可见的按钮将不可见

3 个答案:

答案 0 :(得分:5)

看看这个

  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处理用户按下按钮

        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;


        }
    }

答案 1 :(得分:1)

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);

            }
        }
    });

答案 2 :(得分:0)

你可以定义一个布尔值并使用onclicklistener作为你的按钮,然后如果用户点击该按钮,布尔值将为false并且他无法编写代码。这是一面旗帜。