如何在按下按钮之前添加等待功能?

时间:2013-06-20 05:37:59

标签: java android button onclicklistener

我想添加一个等待,Button(回车)按下功能到我的代码但是我还是新手。 我知道我的代码中有一些错误我正在玩它,但我想要做的是当我按 Button我希望它显示输入X,Y,然后等待直到输入按下以执行我要添加的其余代码。如何在我的代码中实现这样的内容?

这是我的MainActivity班级:

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button enter = (Button) findViewById(R.id.enter);
    Button line = (Button) findViewById(R.id.line);
    Button arc = (Button) findViewById(R.id.arc);

    line.setOnClickListener(this);
    enter.setOnClickListener(this);
    arc.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    TextView vector = (TextView) findViewById(R.id.point);
    TextView index = (TextView) findViewById(R.id.index);
    TextView info = (TextView) findViewById(R.id.info);
    EditText cl = (EditText) findViewById(R.id.editText1);
    DrawingUtils call = new DrawingUtils();
    switch (v.getId()) {
    case R.id.line:
        info.setText("Input X,Y,Z");
        // This Is Where the Wait Function Will GO
        vector.setText(call.addVertice());
        index.setText("1");

        break;
    case R.id.enter:
        String In = cl.getText().toString();
        call.setInputCoords(In);
        break;
    case R.id.arc:
        info.setText("Enter Vertice1 ");
        // Code for entering Vertice1(Also has wait function)
        info.setText("Enter Vertice2");
        // Code for entering Vertice2(Also has wait function)
        info.setText("Enter Height");
        //Code for entering Height(Also has wait function)

    }

}

}

这是我的DrawingUtils班级:

public class DrawingUtils {
String inputCoords;
String[] vertice;

public String getInputCoords() {
    return inputCoords;
}

public void setInputCoords(String inputCoords) {
    this.inputCoords = inputCoords;
}

public String addVertice() {
    int i = 0;
    vertice = inputCoords.split(",");
    return vertice[i];

}

}

1 个答案:

答案 0 :(得分:0)

认为这就是你所追求的。如果没有道歉!

使用布尔标志来处理应用中的状态。这样,如果发生了某些事情,您可以执行不同的代码。

boolean enterPressed = false;    

@Override
public void onClick(View v) {
    TextView vector = (TextView) findViewById(R.id.point);
    TextView index = (TextView) findViewById(R.id.index);
    TextView info = (TextView) findViewById(R.id.info);
    EditText cl = (EditText) findViewById(R.id.editText1);
    DrawingUtils call = new DrawingUtils();
    switch (v.getId()) {
    case R.id.line:
        if (enterPressed) {
            vector.setText(call.addVertice());
            index.setText("1");
        }
        else {
            info.setText("Input X,Y,Z");
        }

        break;
    case R.id.enter:
        String In = cl.getText().toString();
        call.setInputCoords(In);
        enterPressed = true;
        break;
    case R.id.arc:
        info.setText("Enter Vertice1 ");
        // Code for entering Vertice1
        info.setText("Enter Vertice2");
        // Code for entering Vertice2

    }

}