实习第四天了,第一天熟悉了一下java,这三天学习了解了一下安卓开发的一些基础知识。做了一个小程序—计算器,以此帖来记录一下。也许也有人可以参考一下)功能真的只有最基本哈哈,最最新手的人可以参考hh
首先是activity_main.xml的布局代码
这是布局的效果,渐变文本框的效果还需要加一个drawable布局资源
文章图片
需要用到drawable里的rectborder.xml如下:
xmlns:android="http://schemas.android.com/apk/res/android" >
接下来是MainActivity.java文件
package com.example.test_0;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button num0_btn;
private Button num1_btn;
private Button num2_btn;
private Button num3_btn;
private Button num4_btn;
private Button num5_btn;
private Button num6_btn;
private Button num7_btn;
private Button num8_btn;
private Button num9_btn;
private Button mult_btn;
private Button divide_btn;
private Button dot_btn;
private Button result_btn;
private Button back_btn;
private Button delete_btn;
private Button plus_btn;
private Button minus_btn;
private TextView textview_1;
private double NO1=0;
private int flag=0;
private int flag_1=0;
private double NO2=0;
private double end=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num0_btn = (Button) findViewById(R.id.num0_btn);
num1_btn = (Button) findViewById(R.id.num1_btn);
num2_btn = (Button) findViewById(R.id.num2_btn);
num3_btn = (Button) findViewById(R.id.num3_btn);
num4_btn = (Button) findViewById(R.id.num4_btn);
num5_btn = (Button) findViewById(R.id.num5_btn);
num6_btn = (Button) findViewById(R.id.num6_btn);
num7_btn = (Button) findViewById(R.id.num7_btn);
num8_btn = (Button) findViewById(R.id.num8_btn);
num9_btn = (Button) findViewById(R.id.num9_btn);
divide_btn = (Button) findViewById(R.id.divide_btn);
mult_btn = (Button) findViewById(R.id.mult_btn);
dot_btn = (Button) findViewById(R.id.dot_btn);
result_btn = (Button) findViewById(R.id.result_btn);
delete_btn = (Button) findViewById(R.id.delete_btn);
back_btn = (Button) findViewById(R.id.back_btn);
plus_btn = (Button) findViewById(R.id.plus_btn);
minus_btn = (Button) findViewById(R.id.minus_btn);
textview_1 = (TextView) findViewById(R.id.textview_1);
num0_btn.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View view) {
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("0");
if (flag==0) {
NO1 = 10 * NO1 + 0;
}
else {
NO2 =10* NO2+0;
}
}
});
num1_btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("1");
if (flag==0) {
NO1 = 10 * NO1 + 1;
}
else {
NO2 =10* NO2+1;
}
}
});
num2_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("2");
if (flag==0) {
NO1 = 10 * NO1 + 2;
}
else {
NO2 =10* NO2+2;
}
}
});
num3_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("3");
if (flag==0) {
NO1 = 10 * NO1 + 3;
}
else {
NO2 =10* NO2+3;
}
}
});
num4_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("4");
if (flag==0) {
NO1 = 10 * NO1 + 4;
}
else {
NO2 =10* NO2+4;
}
}
});
num5_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("5");
if (flag==0) {
NO1 = 10 * NO1 + 5;
}
else {
NO2 =10* NO2+5;
}
}
});
num6_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("6");
if (flag==0) {
NO1 = 10 * NO1 + 6;
}
else {
NO2 =10* NO2+6;
}
}
});
num7_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("7");
if (flag==0) {
NO1 = 10 * NO1 + 7;
}
else {
NO2 =10* NO2+7;
}
}
});
num8_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("8");
if (flag==0) {
NO1 = 10 * NO1 + 8;
}
else {
NO2 =10* NO2+8;
}
}
});
num9_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//System.out.println(textview_1.getText());
if (flag_1!=0)
{
textview_1.setText("");
flag_1=0;
}
textview_1.append("9");
if (flag==0) {
NO1 = 10 * NO1 + 9;
}
else {
NO2 =10* NO2+9;
}
}
});
delete_btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
if (textview_1.getText()=="0")
{
textview_1.setText("");
}
textview_1.setText("");
flag=0;
}
});
plus_btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
flag=1;
flag_1=1;
// textview_1.setText("");
}
});
mult_btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
flag=3;
flag_1=1;
//textview_1.setText("");
}
});
divide_btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
flag=4;
flag_1=1;
//textview_1.setText("");
}
});
minus_btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
flag=2;
flag_1=1;
//textview_1.setText("");
}
});
result_btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
if (flag==1)
{end=NO1+NO2;
textview_1.setText((String.valueOf(end)));
NO1=0;
NO2=0;
}
else if (flag==3)
{
end=NO1*NO2;
textview_1.setText((String.valueOf(end)));
NO1=0;
NO2=0;
}
else if (flag==2)
{
end=NO1-NO2;
textview_1.setText((String.valueOf(end)));
NO1=0;
NO2=0;
}
else if (flag==4)
{
end=NO1/NO2;
textview_1.setText((String.valueOf(end)));
NO1=0;
NO2=0;
}
}
});
//divide_btn.setOnClickListener(this);
//mult_btn.setOnClickListener(this);
//dot_btn.setOnClickListener(this);
//result_btn.setOnClickListener(this);
}}
【笔记|Android开发实战——计算器】然后这个最最最最基础的计算器就做完啦。 可以进行最简单的加减乘除,每次结果算出来之后得按清除才可以进行下一次计算hh
推荐阅读
- Android小项目——仿iPhone计算器
- Android|Android——一个神奇的计算器APP
- 笔记|王道计算机网络学习笔记
- javascript|JS内置对象(各种案例)
- 笔记|咖啡汪对敖丙老哥Java后端面试心得体会————阿里一面
- docket
- 要把微博、贴吧变成即时聊天,总共分几步()
- 笔记|软考-2021年上半年中级软件设计师备考经验分享(已过!)
- 笔记(C++模板梳理)