输入框输入数字生成条形统计图

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private EditText mEditText1;
    private EditText mEditText2;
    private EditText mEditText3;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int trim1 = Integer.valueOf(mEditText1.getText().toString().trim());
                int trim2 = Integer.valueOf(mEditText2.getText().toString().trim());
                int trim3 = Integer.valueOf(mEditText3.getText().toString().trim());

                ArrayList<Integer> list = new ArrayList<>();

                if (trim1<0||trim1>100){
                    Toast.makeText(MainActivity.this,"只能输入0-100的数字",Toast.LENGTH_SHORT).show();
                    mEditText1.setText("0");
                    list.add(0);
                }else {
                    list.add(trim1);
                }

                if (trim2<0||trim2>100){
                    Toast.makeText(MainActivity.this,"只能输入0-100的数字",Toast.LENGTH_SHORT).show();
                    mEditText2.setText("0");
                    list.add(0);
                }else {
                    list.add(trim2);
                }

                if (trim3<0||trim3>100){
                    Toast.makeText(MainActivity.this,"只能输入0-100的数字",Toast.LENGTH_SHORT).show();
                    mEditText3.setText("0");
                    list.add(0);
                }else {
                    list.add(trim3);
                }


                Intent intent = new Intent(MainActivity.this, SecondActivity.class);

                intent.putIntegerArrayListExtra("a",list);


                startActivity(intent);
            }
        });

    }

    private void findViews() {
         mEditText1 = findViewById(R.id.e1);
        mEditText2 = findViewById(R.id.e2);
        mEditText3 = findViewById(R.id.e3);

        /*mEditText1.addTextChangedListener(new AbsListView(getApplicationContext()) {
            @Override
            public ListAdapter getAdapter() {
                return null;
            }

            @Override
            public void setSelection(int i) {

            }
        });*/
         mButton = findViewById(R.id.btn);
    }
}

activity_main.xml

<LinearLayout 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="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">


    <EditText
        android:id="@+id/e1"
        android:layout_width="200dp"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"
        android:inputType="number"
        android:layout_height="40dp" />
    <EditText
        android:id="@+id/e2"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:inputType="number"
        android:layout_marginTop="50dp"/>
    <EditText
        android:id="@+id/e3"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:inputType="number"
        android:layout_marginTop="50dp"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:text="进入条形图"/>

</LinearLayout>

SecondActivity.java

public class SecondActivity extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        Intent intent = getIntent();
        ArrayList<Integer> a1 = intent.getIntegerArrayListExtra("a");

        int aa = a1.get(0);
        int bb = a1.get(1);
        int cc = a1.get(2);

        int[] ints=new int[]{aa,bb,cc};

        CustomView customView = findViewById(R.id.cv);
        customView.setNum(ints);

    }
}

second.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fanruiqi.bwie.com.jx.CustomView
        android:id="@+id/cv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

CustomView.java

public class CustomView extends View{
    Paint paint;
    int[] mFloats=new int[]{};

    public CustomView(Context context) {
        this(context,null);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();

    }

    private void initData() {
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.FILL);

    }

    public void setNum(int[] floats){

        this.mFloats=floats;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        canvas.drawLine(30,30,30,500,paint);
        canvas.drawLine(30,500,500,500,paint);
        paint.setColor(Color.RED);
        canvas.drawRect(50, (float) (500-mFloats[0]*4.7),80,500,paint);
        paint.setColor(Color.YELLOW);
        canvas.drawRect(150, (float) (500-mFloats[1]*4.7),180,500,paint);
        paint.setColor(Color.GRAY);
        canvas.drawRect(250, (float) (500-mFloats[2]*4.7),280,500,paint);

        //canvas.drawCircle(30,30,30,paint);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值