简体   繁体   中英

How to add a wait function till a button is pressed?

I want to add a wait till, Button (Enter) is pressed function to my code But i am still new to this. I know there are some errors in my code I was playing around with it, But what I want to do is when I press the line Button I want it to display Input X,Y,Z then wait till enter is pressed to execute the rest of my code I want to add in. How would I implement something like this in my code?

Here is my MainActivity Class:

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)

    }

}

}

Here is my DrawingUtils Class:

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

}

}

I think this is what you are after. Apologies if not!

Use a boolean flag to handle state within your app. This way you can execute different code if something has happened.

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

    }

}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM