[英]How to make a piece of code wait till I enter some value in a text view?
我想讓控件執行前兩行(在單行注釋之前),然后等待直到在文本視圖中輸入一些值。我試圖通過單擊onClick
事件名稱為yRaiseTox
的Y^X
按鈕來實現y^x
。
public void yRaiseTox(View view){
/**
* on pressing this button following things are taking place
* 1. Getting string from the text field
* 2.Converting it into Integer to perform math operations
* 3.Setting text field to null
* 4.User will enter a value that will be x
* 5.Using pow() to get the result and printing it back to the text field*/
Integer y = Integer.parseInt(tv1.getText().toString());
tv1.setText("");
// Now I want this code to be executed when I enter some value in the text view tv1
Integer x = Integer.parseInt(tv1.getText().toString());
tv1.setText(pow(y, x)+"");
}
首先,用戶無法輸入文本視圖,而您需要的是edittext。 您可以將兩個不同的代碼放入if語句中,並讓它們在不同的條件下執行,但是用戶將不得不按兩次該按鈕。 如果您不想讓用戶必須按兩次,那么您還可以在執行后設置onTextChanged偵聽器
Integer y = Integer.parseInt(tv1.getText().toString());
tv1.setText("");
然后在更改文本后執行您要執行的代碼的下一部分。
乍一看,創建計算器似乎很容易,但事實並非如此。 我建議您使用State設計模式 。 我已經寫了一些實現。
創建您的狀態界面
public interface State { String getDisplayValue(); State onButtonPressed(int buttonId); }
在這里,我創建了兩種方法。 首先返回字符串以顯示它。 當用戶單擊任何按鈕時,您將按鈕的id傳遞給第二種方法,並通過給定的id返回計算器的新狀態。 當處於新狀態時,您可以向用戶顯示其值,以等待用戶的任何操作
創建用於操作和執行pow操作的界面
public interface Operation { enum Type { ADD, SUBTRACT, MULTIPLY, DIVIDE, SQRT, SQUARE, POW, NEGATE } BigDecimal getResult(); } public class PowOperation implements Operation { private final BigDecimal base; private final BigDecimal power; public PowOperation(BigDecimal base, BigDecimal power) { this.base = base; this.power = power; } @Override public BigDecimal getResult() { return BigDecimalMath.pow(base, power); } }
創建狀態的實現
public class AcState implements State { @Override public String getDisplayValue() { return "0"; } @Override public State onButtonPressed(int buttonId) { switch (buttonId) { case R.id.button_0: case R.id.button_1: case R.id.button_2: case R.id.button_3: case R.id.button_4: case R.id.button_5: case R.id.button_6: case R.id.button_7: case R.id.button_8: case R.id.button_9: return new ValueState(buttonId); // TODO return another states according which button is pressed default: return new AcState(); } } } public class ValueState implements State { private BigDecimal value; private NumberFormat valueFormatter = new DecimalFormat("# ###.###"); public ValueState(int initalValue) { value = BigDecimal.valueOf(initalValue); } @Override public String getDisplayValue() { return valueFormatter.format(value); } @Override public State onButtonPressed(int buttonId) { // TODO not implemented } } public class OperationState implements State { private final BigDecimal value; private final Operation.Type operationType; private NumberFormat valueFormatter = new DecimalFormat("# ###.###"); public OperationState(BigDecimal value, Operation.Type operationType) { this.value = value; this.operationType = operationType; } @Override public String getDisplayValue() { return valueFormatter.format(value); } @Override public State onButtonPressed(int buttonId) { switch (buttonId) { case R.id.button_0: case R.id.button_1: case R.id.button_2: case R.id.button_3: case R.id.button_4: case R.id.button_5: case R.id.button_6: case R.id.button_7: case R.id.button_8: case R.id.button_9: return new SecondValueState(value, operationType, buttonId); } } public class SecondValueState extends ValueState { private final BigDecimal firstValue; private final Operation.Type operationType; public SecondValueState(BigDecimal firstValue, Operation.Type operationType, int initalValue) { super(initalValue); this.firstValue = firstValue; this.operationType = operationType; } @Override public State onButtonPressed(int buttonId) { switch (buttonId) { case R.id.button_equals: return performOperation(); // TODO implement here handling for other buttons } } protected State performOperation() { Operation operation = null; switch (operationType) { case Operation.Type.POW: operation = new PowOperation(BigDecimal firstValue, BigDecimal secondValue); break; // TODO implement here creation other operations according operation type } return new ResultState(operation != null ? operation.getResult() : BigDecimal.ZERO); } } public class ResultState extends State { private final BigDecimal resultValue; private NumberFormat valueFormatter = new DecimalFormat("# ###.###"); public ResultState(BigDecimal resultValue) { this.resultValue = resultValue; } @Override public String getDisplayValue() { return valueFormatter.format(value); } @Override public State onButtonPressed(int buttonId) { // TODO not implemented } }
在上面的代碼中,將AcState
設置為初始狀態。 當用戶單擊任何按鈕時, AcState
返回ValueState
(用戶輸入一些數字)。 在ValueState
如果用戶單擊任何操作按鈕(如加,減等),則將返回OperationState
,其中firstValue是固定的,並且用戶已選擇操作。 當用戶再次開始輸入數字時, OperationState
返回SecondValueState
。
最后,當用戶按下等於按鈕時, SecondValueState
執行計算並返回ResultState
這種模式的優點是所有邏輯都被分成小類,並且易於擴展。 缺點是您必須編寫很多代碼!
設置布爾值GettingY並將其設置為true。 第一次單擊y ^ x按鈕,它將執行第一部分;第二次單擊時,它將執行第二部分。
public void yRaiseTox(View view){
if(gettingY){
Integer y = Integer.parseInt(tv1.getText().toString());
tv1.setText("");
gettingY = false;
} else {
Integer x = Integer.parseInt(tv1.getText().toString());
tv1.setText(pow(intValue, valueOfx)+"");
gettingX = true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.