Android动态按钮要擦除的按钮-获得解决方案
Lastbutton.setVisibility(视图.不可见);-解决方案 我正在尝试下面的一些数学计算Android动态按钮要擦除的按钮-获得解决方案,android,button,Android,Button,Lastbutton.setVisibility(视图.不可见);-解决方案 我正在尝试下面的一些数学计算 4 2 8 3 ( Displayed in Button ) + - * / ( Displayed in Button ) 10 假设用户按4+2,然后像下面一样。(第一个按下的按钮将被移除) 假设用户按6,然后按3,如下
4 2 8 3 ( Displayed in Button )
+ - * / ( Displayed in Button )
10
假设用户按4+2,然后像下面一样。(第一个按下的按钮将被移除)
假设用户按6,然后按3,如下图所示。(第一个按下的按钮将被移除)
假设用户按18,然后按8,就像下面一样
4 2 8 3 ( Displayed in Button )
+ - * / ( Displayed in Button )
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();
}
}
}
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm=(线性布局)findViewById(R.id.lm);
//为外部LinearLayout内的每一行创建LinearLayout。
LinearLayout firstRow=新的LinearLayout(本);
第一行。设置方向(线性布局。水平);
//创建要添加到内部LinearLayout的项目的字符串数组。
字符串[]itemsToAddToFirstRow={“4”、“2”、“8”、“3”};
动态创建按钮(第一行,itemsToAddToFirstRow);
字符串[]itemsToAddToSecondRow={“+”、“-”、“*”、“/”};
LinearLayout secondRow=新的LinearLayout(此);
第二行。设置方向(线性布局。水平);
动态创建按钮(第二行,itemsToAddToSecondRow);
}
私有void CreateButtonsDynamic(LinearLayout LayoutLoaddButtonsTo,String[]itemsToAdd){
for(int i=0;i
我试过了,但它不起作用。
你能再解释一下它到底做了什么吗?我试过移除view()按钮,但它强制停止了。不知道为什么。