Lastbutton.setVisibility(View.INVISIBLE); - 解决方案。
我正在尝试下面的数学计算。
4 2 8 3 ( Displayed in Button )
+ - * / ( Displayed in Button )
假设用户按下4然后按+然后按2然后如下所示。 (第一个按下按钮)
6 8 3
假设用户按下6然后按*然后按3然后如下所示。 (第一个按下按钮)
8 18
假设用户按18然后 - 然后8然后如下。
10
我试过了,它不起作用。我也粘贴了我当前版本的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LinearLayout) findViewById(R.id.lm);
// Create a LinearLayout for each row inside the outer LinearLayout.
LinearLayout firstRow = new LinearLayout(this);
firstRow.setOrientation(LinearLayout.HORIZONTAL);
// Create a String array of items you want to add to the inner LinearLayout.
String[] itemsToAddToFirstRow = { "4", "2", "8", "3"};
createButtonsDynamically(firstRow, itemsToAddToFirstRow);
String[] itemsToAddToSecondRow = { "+", "-", "*", "/" };
LinearLayout secondRow = new LinearLayout(this);
secondRow.setOrientation(LinearLayout.HORIZONTAL);
createButtonsDynamically(secondRow, itemsToAddToSecondRow);
}
private void createButtonsDynamically(LinearLayout layoutToAddButtonsTo, String[] itemsToAdd) {
for (int i = 0; i < itemsToAdd.length; i++) {
final Button buttonToAdd = new Button(this);
buttonToAdd.setText(itemsToAdd[i]);
buttonToAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Strvalue = (String) buttonToAdd.getText();
operation(buttonToAdd);
}
});
layoutToAddButtonsTo.addView(buttonToAdd);
}
// In the end add all buttons inside the inner LinearLayout to the outer LinearLayout.
lm.addView(layoutToAddButtonsTo);
}
private void operation(Button buttonToAdd)
{
if (Strvalue.equals("Clear"))
{
buttonToAdd.setEnabled(true);
Toast.makeText(getApplicationContext(), "I was clicked: " + Strvalue , Toast.LENGTH_SHORT).show();
}
else
{
if (Strvalue.equals("+") || Strvalue.equals("-") || Strvalue.equals("*")|| Strvalue.equals("/"))
{
flag = true;
Toast.makeText(getApplicationContext(), "I was clicked: " + Strvalue , Toast.LENGTH_SHORT).show();
Pressoperator = Strvalue;
product1.setText("Calc Answer: "+ Calctotal);
}
else
{
flag = false;
if (Pressoperator.equalsIgnoreCase(Emptystr))
{
Calctotal = Integer.parseInt(Strvalue);
Prevvalue = Integer.parseInt(Strvalue);
Toast.makeText(getApplicationContext(), "I was clicked: " + Calctotal , Toast.LENGTH_SHORT).show();
product1.setText(Integer.toString(Calctotal));
}
else
{
if (Pressoperator.equals("+")) { Calctotal = Prevvalue + Integer.parseInt(Strvalue); }
if (Pressoperator.equals("-")) { Calctotal = Prevvalue - Integer.parseInt(Strvalue); }
if (Pressoperator.equals("*")) { Calctotal = Prevvalue * Integer.parseInt(Strvalue); }
if (Pressoperator.equals("/")) { Calctotal = Prevvalue / Integer.parseInt(Strvalue); }
Toast.makeText(getApplicationContext(), "I was clicked: " + Calctotal , Toast.LENGTH_SHORT).show();
Prevvalue = Calctotal;
product1.setText(Integer.toString(Calctotal));
}
if (Calctotal == total)
{
Toast.makeText(getApplicationContext(), " Yureka Your Answer is Correct " , Toast.LENGTH_SHORT).show();
}
}
}
}