我创建了简单的计算器应用程序,我想在它下面画一个矩形。矩形边的大小必须是为计算输入的第一个数字乘以10.我不知道我是否正确行事,但我试图在LinearLayout中绘制浅蓝色的矩形。单击“立即绘制”按钮后,必须绘制矩形。运行应用程序后,输入数字并单击立即绘制LinearLayout的颜色消失。
这是我的MainActivity:
package com.example.whodatboy.calculator;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
TextView result;
EditText number1, number2;
Button addN,subtract,divide,multiplyN,clearAll,drawMe;
LinearLayout rect;
int result_num,num1,num2;
float numm1, numm2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
result = (TextView)findViewById(R.id.result);
addN = (Button)findViewById(R.id.addN);
subtract = (Button)findViewById(R.id.subtract);
divide = (Button)findViewById(R.id.divide);
multiplyN = (Button)findViewById(R.id.multiplyN);
clearAll = (Button)findViewById(R.id.clearAll);
drawMe = (Button)findViewById(R.id.drawMe);
addN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 + num2;
result.setText(String.valueOf(result_num));
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 - num2;
result.setText(String.valueOf(result_num));
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 / num2;
result.setText(String.valueOf(result_num));
}
});
multiplyN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 * num2;
result.setText(String.valueOf(result_num));
}
});
clearAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number1.setText("");
number2.setText("");
result.setText("");
}
});
drawMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numm1 = Float.parseFloat(number1.getText().toString());
Paint paint = new Paint();
paint.setColor(Color.parseColor("#F4D03F"));
Bitmap bg = Bitmap.createBitmap(480,800,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bg);
canvas.drawRect(num1*10,num1*10,num1*10,num1*10,paint);
LinearLayout linearDraw = (LinearLayout)findViewById(R.id.rect);
linearDraw.setBackground(new BitmapDrawable(bg));
}
});
}
}
这是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number 1"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:ems="10"
android:hint="Number 2"
android:inputType="number" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/number2"
android:text="Result"
android:textSize="25sp" />
<Button
android:id="@+id/addN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/subtract"
android:layout_toEndOf="@+id/divide"
android:text="+" />
<Button
android:id="@+id/subtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/multiplyN"
android:layout_toEndOf="@+id/multiplyN"
android:text="-" />
<Button
android:id="@+id/multiplyN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="122dp"
android:text="*" />
<Button
android:id="@+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/subtract"
android:layout_toEndOf="@+id/subtract"
android:text="/" />
<Button
android:id="@+id/clearAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/addN"
android:text="Clear" />
<LinearLayout
android:id="@+id/rect"
android:layout_width="match_parent"
android:layout_height="238dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="9dp"
android:layout_marginStart="0dp"
android:background="#89C4F4"
android:orientation="vertical">
</LinearLayout>
<Button
android:id="@+id/drawMe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="@+id/clearAll"
android:layout_marginEnd="0dp"
android:text="Draw NOW" />
</RelativeLayout>
答案 0 :(得分:0)
您可以尝试将height
和width
设为num1*10
LinearLayout layout(LinearLayout)findViewById(R.id.rect);
layout.getLayoutParams().height=num1*10;
layout.getLayoutParams().width=num1*10;
layout.setBackgroundColor(Color.parseColor("#F4D03F"));