我使用视频教程制作计算器。在视频教程中只使用一个布局。所有按钮都保存在一个布局中。但我想使用多个布局并在一个布局中保留行。但它不起作用。
我的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.saddam.calculator.MainActivity"
android:background="#1C2836"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linnearLayoutCalculator">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#7FA1FF"
android:layout_marginBottom="10dp"
android:id="@+id/textViewOutputScreen"
android:textSize="50sp"
android:gravity="right"
android:textColor="#FFFFFF"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linear1"
>
<Button
android:text="7"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey7"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="8"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey8"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="9"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey9"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="/"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKeyDivide"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
>
<Button
android:text="4"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey4"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="5"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey5"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="6"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey6"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="*"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKeyMultiply"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
<Button
android:text="1"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey1"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="2"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey2"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="3"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey3"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="-"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKeySubtract"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
<Button
android:text="0"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey0"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="."
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKey_dot"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="+"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKeyAdd"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="="
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKeyEquals"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
>
<Button
android:text="Clear"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/buttonKeyC"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
android:layout_marginEnd="5dp"
/>
<Button
android:text="%"
android:layout_width="0dp"
android:layout_height="60dp"
android:id="@+id/btn_percentage"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#69C1B4"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是我的MainActivity.java代码
package com.saddam.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button button = (Button)findViewById(R.id.buttonKey7);
// button.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
// Toast.makeText(getApplicationContext(), "Java Hookup, button clicked: " + ((Button) v).getText(), Toast.LENGTH_SHORT).show();
// }
// });
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linnearLayoutCalculator);
int count = linearLayout.getChildCount();
ButtonClickHandler buttonClickHandler = new ButtonClickHandler();
for (int j=0;j<count;j++){
View v = linearLayout.getChildAt(j);
if(v instanceof Button) {
v.setOnClickListener(buttonClickHandler);
}
}
}
private class ButtonClickHandler implements View.OnClickListener
{
@Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(), "Java Hookup, button clicked: " + ((Button) v).getText(), Toast.LENGTH_SHORT).show();
TextView textViewOutputScreen = (TextView)findViewById(R.id.textViewOutputScreen);
// A little defensive programming here--we expect that all clicked things are buttons
// but just in case, we have this check here.
if(v instanceof Button){
Button buttonClicked = (Button)v;
// Check for the clear button. If clear button pressed, reset calculator screen to zero
if(v.getId() == R.id.buttonKeyC){
textViewOutputScreen.setText("0");
}
// Check for the equals sign. If pressed, then calculate our result
else if(v.getId() == R.id.buttonKeyEquals){
try {
double calcResult = CalCulator.evaluate(textViewOutputScreen.getText().toString());
textViewOutputScreen.setText(Double.toString(calcResult));
}
catch(Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
textViewOutputScreen.setText("0");
}
}
// Check if two math operators pressed in a row. If so, show an error message using Toast
else if(textViewOutputScreen.getText().length() > 0 &&
CalCulator.isOperator(textViewOutputScreen.getText().charAt(textViewOutputScreen.getText().length() - 1)) &&
CalCulator.isOperator(buttonClicked.getText().charAt(0)))
{
String errorMessage = "You cannot enter two math operators in a row. You entered " + textViewOutputScreen.getText().charAt(textViewOutputScreen.getText().length() - 1) + " and " + buttonClicked.getText() + ".";
Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_LONG).show();
}
// Else, concatenate current button text to calculator screen
else {
if (textViewOutputScreen.getText().equals("0") &&
!CalCulator.isOperator(buttonClicked.getText().charAt(0))) {
textViewOutputScreen.setText("");
}
textViewOutputScreen.setText(textViewOutputScreen.getText().toString() + buttonClicked.getText());
}
}
}
}
}
这里是我的Calculator.java代码
package com.saddam.calculator;
/**
* Created by saddam019 on 11/12/2016.
*/
public final class CalCulator {
private CalCulator(){
}
public static boolean isOperator(char c){
return c == '/' ||
c == '÷' ||
c == '*' ||
c == '-' ||
c == '+';
}
// This code parses a arithmetic string (with *, +, -, and / operators supported) and provides
// a proper mathematical result. If the string is not parsable, throws RuntimeException
// Code from: http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form
public static double evaluate(final String str){
class Parser {
int pos = -1, c;
void eatChar() {
c = (++pos < str.length()) ? str.charAt(pos) : -1;
}
void eatSpace() {
while (Character.isWhitespace(c)) eatChar();
}
double parse() {
eatChar();
double v = parseExpression();
if (c != -1) throw new RuntimeException("Unexpected: " + (char)c);
return v;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor | term brackets
// factor = brackets | number | factor `^` factor
// brackets = `(` expression `)`
double parseExpression() {
double v = parseTerm();
for (;;) {
eatSpace();
if (c == '+') { // addition
eatChar();
v += parseTerm();
} else if (c == '-') { // subtraction
eatChar();
v -= parseTerm();
} else {
return v;
}
}
}
double parseTerm() {
double v = parseFactor();
for (;;) {
eatSpace();
if (c == '/' || c == '÷') { // division
eatChar();
v /= parseFactor();
} else if (c == '*' || c == '(') { // multiplication
if (c == '*') eatChar();
v *= parseFactor();
} else {
return v;
}
}
}
double parseFactor() {
double v;
boolean negate = false;
eatSpace();
if (c == '+' || c == '-') { // unary plus & minus
negate = c == '-';
eatChar();
eatSpace();
}
if (c == '(') { // brackets
eatChar();
v = parseExpression();
if (c == ')') eatChar();
} else { // numbers
int startIndex = this.pos;
while ((c >= '0' && c <= '9') || c == '.') eatChar();
if (pos == startIndex) throw new RuntimeException("Unexpected: " + (char)c);
v = Double.parseDouble(str.substring(startIndex, pos));
}
eatSpace();
if (c == '^') { // exponentiation
eatChar();
v = Math.pow(v, parseFactor());
}
if (negate) v = -v; // unary minus is applied after exponentiation; e.g. -3^2=-9
return v;
}
}
return new Parser().parse();
}
}