Android 项目开发实战之不确定度计算(主要运用Java动态添加控件知识)

思路:
1.构造好布局
布局构造其实不难
代码如下:
1.总布局肯定是ScrollView
第一个LinearLayout 是在ScrollView子布局,接下来写了一个Button来获取常见不确定度的常用数据,转向另一个布局。
为了美观才写了这么多的LinearLayout
其余就不用细说了观察图片就知道了,
但有一个重点来了 一个名字叫做android:id="@+id/All"的布局相当重要
他是同来动态添加布局的主角。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/timg2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/huoqu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25dp"
            android:background="@drawable/ids"
            android:text="获取常见仪器误差" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            tools:context=".MainActivity">

            <TextView
                android:id="@+id/Textmin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="仪器误差:"

                android:textSize="30sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText
                android:id="@+id/textMin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            tools:context=".MainActivity">

            <TextView
                android:id="@+id/TT"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="数据组数:"

                android:textSize="30sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText
                android:id="@+id/TextNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="number" />


        </LinearLayout>
        <Button
            android:id="@+id/inpubutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="填写数据!"
            android:background="@drawable/ids"
            android:textSize="35sp" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/All"
            >
        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textshuju"
            android:textSize="30dp"/>
    </LinearLayout>
</ScrollView>

示意图:
在这里插入图片描述

2.下面就来写Java代码

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jiandankonjian.activity.liebiaoActivity;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

    int editNumber = 0 ;
    TextView textViews[];
    EditText editTexts[];
    LinearLayout linearLayouts[];
    double sB ;
    double A ;
    double B ;
    double C ;
    double vavge ;
    TextView textView ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button huoqu = findViewById(R.id.huoqu);
        huoqu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, liebiaoActivity.class);
                startActivity(intent);
            }
        });
        //获取确认数据个数按钮,获取格数文本
        Button inputbutton = findViewById(R.id.inpubutton);
        final EditText TextNumber = findViewById(R.id.TextNumber);
        final EditText textmin = findViewById(R.id.textMin);
        final LinearLayout All = findViewById(R.id.All);
        textView = findViewById(R.id.textshuju);
        // 设置buttonsure的属性
        final Button buttonSure = new Button(MainActivity.this);
        buttonSure.setText("计算");
        buttonSure.setTextSize(30);
        buttonSure.setBackground(getDrawable(R.drawable.ids));
        buttonSure.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        buttonSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(editNumber == 0) editNumber++;
                double sum = 0;
                double a[] = new double[editNumber];
                 for(int i = 0 ; i < editNumber ; i++){
                     if(editTexts[i].length() == 0) {
                         Toast.makeText(MainActivity.this, ("数据"+(i+1)+"不能为空"), Toast.LENGTH_SHORT).show();
                         return;
                     }
                     a[i] = Double.valueOf(editTexts[i].getText().toString());
                 }
                 textView.setBackgroundColor(R.drawable.dingzhi);
                 vavge = getVariage(a,editNumber);
                 A =getVA(a,editNumber,vavge);
                 B = getVB(sB);
                 C = getVC(A,B);
                 textView.setText("平均值:"+vavge+"\nA类不确定度:"+A+"\nB类不确定度:"+B+"\nC类不确定度:"+C);

            }
        });
        //添加点击事件和添加刷新事件
        inputbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(textmin.length()==0){
                    Toast.makeText(MainActivity.this,"仪器误差不能为空!",Toast.LENGTH_SHORT).show();
                    return;
                }
                if((TextNumber.length() == 0 )||Integer.parseInt(TextNumber.getText().toString())<2 ){
                    Toast.makeText(MainActivity.this, "数据不能少于2组", Toast.LENGTH_SHORT).show();
                    return;
                }
                All.removeAllViews();
                editNumber = Integer.parseInt(TextNumber.getText().toString());
                textViews = new TextView[editNumber];
                editTexts = new EditText[editNumber];
                linearLayouts = new LinearLayout[editNumber];
                sB = Double.valueOf(textmin.getText().toString());
               
                for(int i = 0 ; i<editNumber ; i++) {
                    editTexts[i] = new EditText(MainActivity.this);
                    linearLayouts[i] = new LinearLayout(MainActivity.this);
                    textViews[i] = new TextView(MainActivity.this);
                    editTexts[i].setSingleLine();
                    textViews[i].setTextSize(30);
                    textViews[i].setWidth(300);
                    String str = "数据"+(i+1)+":";
                    textViews[i].setText(str);
                    editTexts[i].setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED);
                    editTexts[i].setTextSize(30);
                    editTexts[i].setWidth(600);
                    linearLayouts[i].addView(textViews[i]);
                    linearLayouts[i].addView(editTexts[i]);
                    All.addView(linearLayouts[i]);
                }
                All.addView(buttonSure);
            }
        });

    }

    double getVariage(double a[],int number){
        double sum = 0 ;
        for (int i = 0 ; i<number ; i++)
        {
            sum += a[i];
        }
        return sum/number;
    }
    double getVA(double a[],int number,double vager)
    {

        double sum = 0 ;
        for (int i = 0 ; i<number; i++)
        {
            sum = sum + ((a[i]-vager)*(a[i]-vager));
        }
        return  sum/number/(number-1);
    }
    double getVB(double b){
        return b/1.7321;
    }
    double getVC(double a,double b){
        return Math.sqrt(a*a+b*b);
    }
}

示意图如下:

在这里插入图片描述
有什么问题可以一起进行研讨哦

发布了30 篇原创文章 · 获赞 62 · 访问量 3097

猜你喜欢

转载自blog.csdn.net/weixin_43981664/article/details/89354822