简体   繁体   中英

android- wait for user in loop unless it clicks button

my code not waiting for user to click and i have no idea how to do it. plz see comments. I am making cards game in which there are 3 computer player and one user. But i don't know how to wait for user to click any card and then play in each iteration of loop.Here is my rough code.Thanks in advance.

inp=0,k=0; //global variable 
function onCreate()
{
added listner to 13 imageviews 
A(); // call to A()
}
function A()
{
// I want to wait here to get updated value of inp when user clicks image to
// perform some operation with inp and remove that imageView.
k++;
if(k<13)
A();


}
function onClick(view)
{
switch:
  case img1:
    inp=0;
    break;
 case img2:
    inp=2;
    break;
 case img3:
    inp=3;
    break;
  .
  .
  .
  .
  13 cases

}

Don't call function A() in onCreate method. Call it whenever click event occurs, call it at end of onClick method after setting inp=something then use that value in function A() do your operation with that value.

Try rxjava, it is quite easy to observe your inp value changed then perform your desire function, for example:

private int inp = 0;
private BehaviorSubject<Integer> intValueObserve;

private void inpValueChangedCallBack() {
    intValueObserve = BehaviorSubject.create(inp);
    intValueObserve.subscribe(new Action1<Integer>() {
        @Override
        public void call(Integer integer) {
            Log.d("TAG", "new value: " + integer.intValue());
            // perform `remove image view` here
        }
    });
}

...

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_activity);

    inpValueChangedCallBack();
}

....

@Click(R.id.add_record)
void add() {
    inp++;
    intValueObserve.onNext(inp); // adding this into the end of your onClick function
    Log.d("TAG", "increase inp: " + inp);
}

And the log as my test:

D/TAG: new value: 1
D/TAG: increase inp: 1
D/TAG: new value: 0
D/TAG: decrease intp: 0
D/TAG: new value: 1
D/TAG: increase inp: 1
D/TAG: new value: 2
D/TAG: increase inp: 2

Hope this help!

Android, like pretty much every GUI OS out there, is event driven. That means there's an event loop in the background. That event loop does all the waiting. You should never be waiting for anything- in fact if you try you'll freeze the UI thread and become unresponsive, never responding to input. The OS will call your click listener that you've registered when the event occurs. That's the basic idea in event driven programming- the OS tells you when events occur.

If you want to wait try using Thread.Sleep();

while(condition == true)
{
    try {
       Thread.Sleep(Time);
         } catch(InterruptedException e){}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM