2011-04-27 20 views
0

一個問題工作一次(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; 
} 

添加(INT麻木)方法添加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; 
} 

ArryayList射線是數被儲存在那裏。計算工作正常。 Int num是代碼使用的編號。布爾準備就緒,因爲在runner類中有一個while循環,等待準備好以繼續執行代碼。

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

任何想法?

編輯:這是在亞軍的方法被稱爲:

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 
+0

什麼是跑步者循環的重點?它不會只是作爲一個無限循環而存在嗎?或者它在異步線程中?處理類似事件的更好方法是使用廣播接收器 – 2011-04-27 18:16:33

+1

什麼部分第二次不工作?錯誤?例外?錯號碼?沒有號碼?無限循環?惡魔? – Ishtar 2011-04-27 18:19:58

+0

@John:我試着暫停它來等待來自主Activity的消息,但是它開始拋出異常中斷。 Runner類的確將消息發送給主類,如果這有幫助的話。 – Travis 2011-04-27 18:22:10

回答

0
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()完成後。當用戶完成輸入並按下一個鍵時,您必須確保正確的代碼執行,然後部分2被調用。然後在需要再次輸入時停止運行代碼。

祝你好運!

+0

感謝Ishtar,這比我做的事更有意義! – Travis 2011-04-29 17:11:21