开发者

Input only working once in Android

A question (ID 1606993) like this was posted, but didn't really answer mine, so here goes:

In an app for Android that I am creating, the user must input numbers from the keypad for the app to continue. When they are done entering their numbers, they can either press the return key or the main button. Problem is, its only working once.

Here is my listening method:

public boolean onKeyDown(int key, KeyEvent event)//Listens for key events
{
    if(key==KeyEvent.KEYCODE_0)
        add(0);
    else if(key==KeyEvent.KEYCODE_1)
        add(1);
    else if(key==KeyEvent.KEYCODE_2)
        add(2);
    else if(key==KeyEvent.KEYCODE_3)
        add(3);
    else if(key==KeyEvent.KEYCODE_4)
        add(4);
    else if(key==KeyEvent.KEYCODE_5)
        add(5);
    else if(key==KeyEvent.KEYCODE_6)
        add(6);
    else if(key==KeyEvent.KEYCODE_7)
        add(7);
    else if(key==KeyEvent.KEYCODE_8)
        add(8);
    else if(key==KeyEvent.KEYCODE_9)
        add(9);
    else if(key==KeyEvent.KEYCODE_ENTER)
        setNum();
    else if(key==KeyEvent.KEYCODE_DPAD_CENTER)
        setNum();
    return true;
}

The add(int numb) method adds num to the text field that takes up the screen, its not causing any problems.

Here is setNum() :

protected void setNum()//Sets the number and tells the Runner it is set.
{
    if(ray.size()==1)
        num=ray.get(0);
    else if(ray.size()==0)
        num=0;
    else
        num=(ray.get(0)*10)+ray.get(1);
    ray=new ArrayList<Integer>();//or ray.clear(), I've tried both
    ready=true;
}

ArryayList ray is where the numbers are stored. The computation is working fine. Int num is the number that the code uses. Boolean ready is there because in the runner class there is a while loop waiting for ready to be true to continue the code.

while(!a.ready)
{
    for(int x=0;x<100;x++);
}

Any ideas?

Edit: Here is the method in Runner that is called:

while(!go)
    {
        addText("It is White's move");
        addText("Possible pieces to move");
        for(int x=0;x<b.getWhite().getPiecesWithMoves(b).size();x++)//Loop to print White's pieces that can move
        {
            addText(""+(x+1)+") "+b.getWhite().getPiecesWithMoves(b).get(x));
        }   
        got=false;
        p=0;
        while(!got)//Loop to enter number
        {
            addText("Input the number of the piece you want to move");
            while(!a.ready)
            {
                for(int x=0;x<开发者_如何学运维100;x++);
            }
            p=a.num-1;
            if(p<b.getWhite().getPiecesWithMoves(b).size()&&p>=0)//Checks to make sure that p is valid
                got=true;
            a.num=0;
            a.ready=false;
        }
        gl=b.getWhite().getPiecesWithMoves(b).get(p).getLocation();//Makes a location that is where the piece currently is
        addText("Possible moves");
        for(int x=0;x<b.getPiece(gl).getMoves(b).size();x++)//Loop to print where the piece can go
        {
            addText(""+(x+1)+") "+b.genL(b.getPiece(gl).getMoves(b).get(x)));
        }
        got=false;//reset
        while(!got)//Loop to enter number
        {
            addText("Input the number of one of these moves.");
            addText("If you wish to change, enter 0.");
            while(!a.ready)
            {
                for(int x=0;x<100;x++);
            }
            p=a.num-1;
            if(p==-1)
                got=true;
            else if(p<b.getPiece(gl).getMoves(b).size()&&p>=0)//Checks to make sure that p is valid
            {
                got=true;
                go=true;
            }
            a.num=0;
            a.ready=false;
        }
    }
    gk=b.getPiece(gl).getMoves(b).get(p);//The location that the piece is going to
    b.move(gk, b.getPiece(gl),gk.isTaken(),gk.isCastle());//Moves the piece


public class MyView {
  int num;
  int currentState = 1; //much, much more pretty if you'd use Enum, 
                        //but I'll keep it simple now.

  public boolean onKeyDown(int key, KeyEvent event) {
    //no changes here
  }

  protected void setNum(){
    if(ray.size()==1)
        num=ray.get(0);
    else if(ray.size()==0)
        num=0;
    else
        num=(ray.get(0)*10)+ray.get(1);
    ray=new ArrayList<Integer>();
    if (currentState == 1) //(TODO:should really use Enum here....)
       part1();
    else if (currentState == 2)
       part2();
    else if (...
  }

  void part1() {
    addText("It is White's move");
    addText("Possible pieces to move");
    for(int x=0;x<b.getWhite().getPiecesWithMoves(b).size();x++)
    {
      addText(""+(x+1)+") "+b.getWhite().getPiecesWithMoves(b).get(x));
    }

    addText("Input the number of the piece you want to move");
    currentState = 2;//now we do nothing, we wait untill user finishes input
  }
  void part2()
  {//will be called after part1 finishes and a number has been entered
    p=a.num-1;
    if(p<b.getWhite().getPiecesWithMoves(b).size()&&p>=0) {
      gl=b.getWhite().getPiecesWithMoves(b).get(p).getLocation();
      addText("Possible moves");
      //and more code
      //Need input? stop and set next entry point
      currentState = 3;//continue to part 3
    }
    else
    {
      addText("Input the number of the piece you want to move");
      currentState = 2;//oops, stay at part2, we didn't get a good number!
    }
  }

}

The idea is to do nothing when the user has to input. There is no code running at that moment, for example after part1() finishes. When the user finishes input and presses a key, you have to make sure the correct code then executes, part2 gets called then. And then just stop running code when you need input again.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜