请给出一个能直接在Android Studio上运行的Android小项目
时间: 2023-08-05 19:31:29 浏览: 80
好的,以下是一个简单的可以直接在Android Studio上运行的Android小项目,它是一个简单的计算器应用程序:
1. 新建一个Android Studio项目,选择 Empty Activity 模板。
2. 打开 activity_main.xml 文件,并添加一个 TextView 和多个 Button:
```xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:padding="10dp"
android:textColor="@android:color/black"
android:background="@android:color/white"
android:gravity="right"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_weight="1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="1"/>
<Button
android:id="@+id/buttonPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_weight="1"/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:layout_weight="1"/>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:layout_weight="1"/>
<Button
android:id="@+id/buttonMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:layout_weight="1"/>
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:layout_weight="1"/>
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:layout_weight="1"/>
<Button
android:id="@+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="@+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:layout_weight="1"/>
<Button
android:id="@+id/buttonZero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:id="@+id/buttonEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:layout_weight="1"/>
<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
```
3. 打开 MainActivity.java 文件,并添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private TextView resultTextView;
private Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0, buttonClear, buttonEqual, buttonPlus, buttonMinus, buttonMultiply, buttonDivide;
private String lastOperation = "";
private boolean isLastOperationEquals = false;
private double lastNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.resultTextView);
button0 = findViewById(R.id.buttonZero);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button5 = findViewById(R.id.button5);
button6 = findViewById(R.id.button6);
button7 = findViewById(R.id.button7);
button8 = findViewById(R.id.button8);
button9 = findViewById(R.id.button9);
buttonClear = findViewById(R.id.buttonClear);
buttonEqual = findViewById(R.id.buttonEqual);
buttonPlus = findViewById(R.id.buttonPlus);
buttonMinus = findViewById(R.id.buttonMinus);
buttonMultiply = findViewById(R.id.buttonMultiply);
buttonDivide = findViewById(R.id.buttonDivide);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
numberClicked("9");
}
});
buttonPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operationClicked("+");
}
});
buttonMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operationClicked("-");
}
});
buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operationClicked("*");
}
});
buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operationClicked("/");
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clearClicked();
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
equalsClicked();
}
});
}
private void numberClicked(String number) {
if (isLastOperationEquals) {
resultTextView.setText("");
isLastOperationEquals = false;
}
String currentResult = resultTextView.getText().toString();
if (currentResult.equals("0")) {
currentResult = "";
}
currentResult += number;
resultTextView.setText(currentResult);
}
private void operationClicked(String operation) {
if (isLastOperationEquals) {
isLastOperationEquals = false;
}
String currentResult = resultTextView.getText().toString();
if (currentResult.equals("")) {
return;
}
double currentNumber = Double.parseDouble(currentResult);
if (!lastOperation.equals("")) {
switch (lastOperation) {
case "+":
lastNumber += currentNumber;
break;
case "-":
lastNumber -= currentNumber;
break;
case "*":
lastNumber *= currentNumber;
break;
case "/":
lastNumber /= currentNumber;
break;
}
resultTextView.setText(Double.toString(lastNumber));
} else {
lastNumber = currentNumber;
}
lastOperation = operation;
}
private void clearClicked() {
lastNumber = 0;
lastOperation = "";
resultTextView.setText("");
}
private void equalsClicked() {
String currentResult = resultTextView.getText().toString();
if (currentResult.equals("")) {
return;
}
double currentNumber = Double.parseDouble(currentResult);
if (!lastOperation.equals("")) {
switch (lastOperation) {
case "+":
lastNumber += currentNumber;
break;
case "-":
lastNumber -= currentNumber;
break;
case "*":
lastNumber *= currentNumber;
break;
case "/":
lastNumber /= currentNumber;
break;
}
resultTextView.setText(Double.toString(lastNumber));
}
isLastOperationEquals = true;
}
}
```
4. 运行该应用程序,您将看到一个简单的计算器应用程序,您可以使用它来进行简单的数学运算。
阅读全文