这样的问题(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;
}
ArryayList 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
答案 0 :(得分: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()完成之后。当用户完成输入并按下某个键时,您必须确保执行正确的代码,然后调用part2。然后在需要再次输入时停止运行代码。
祝你好运!