【问题标题】:Input only working once in Android输入只能在 Android 中工作一次
【发布时间】:2011-04-27 18:07:05
【问题描述】:

这样的问题(ID 1606993)已发布,但并没有真正回答我的问题,所以这里是:

在我正在创建的 Android 应用中,用户必须从键盘输入数字才能继续应用。当他们输入完号码后,他们可以按返回键或主按钮。问题是,它只能工作一次。

这是我的听力方法:

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

add(int numb) 方法将 num 添加到占据屏幕的文本字段中,不会造成任何问题。

这里是 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;
}

ArrayyList ray 是存储数字的位置。计算工作正常。 Int num 是代码使用的数字。布尔就绪是因为在 runner 类中有一个 while 循环等待 ready 为 true 以继续代码。

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

有什么想法吗?

编辑:这是 Runner 中被调用的方法:

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

【问题讨论】:

  • 跑步者循环的意义何在?它不会只是作为无限循环存在吗?还是在异步线程中?处理此类事件的更好方法是使用广播接收器
  • 第二次哪个部分不工作?错误?例外?错号码?没有号码?无限循环?恶魔?
  • @John:我尝试暂停它以等待来自主 Activity 的消息,但它开始引发中断异常。 Runner 类确实会向主类发送消息,如果这有帮助的话。
  • 尝试调试 setNum 函数,看看 ray 里面有什么。只需在您的设备上以调试模式启动应用程序,并在 if 语句的顶部设置断点
  • 尽可能多的。我们随时为您提供帮助。

标签: java android input


【解决方案1】:
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!
    }
  }

}

这个想法是当用户必须输入时什么都不做。那时没有代码在运行,例如在 part1() 完成之后。当用户完成输入并按下一个键时,您必须确保执行正确的代码,然后调用 part2。然后在您需要再次输入时停止运行代码。

祝你好运!

【讨论】:

  • 感谢 Ishtar,这比我所做的更有意义!
猜你喜欢
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多