android - 按钮不在计算器应用程序中工作

时间:2017-04-10 22:41:28

标签: android android-studio button textview

我已经构建了一个计算器应用程序,它使用EditTexts为您插入两个值,然后我还有4个按钮,每个按钮都有一个操作(例如+, - ,/,x)。看起来好像按钮没有分配给它的功能,没有值被发送到TextView。你能帮我把这些按钮做好吗?

以下是我的代码:

package com.example.user.zidcalculator;

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.TextView;

public class MainActivity extends AppCompatActivity {

    int enter1;
    int enter2;
    int newHeight;
    EditText et1;
    EditText et2;
    TextView tv;
    Button bt1;
    Button bt2;
    Button bt3;
    Button bt4;

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

        et1 = (EditText) findViewById(R.id.edittext1);
        et2 = (EditText) findViewById(R.id.edittext2);
        tv = (TextView) findViewById(R.id.tvans);
        bt1 = (Button) findViewById(R.id.button);
        bt2 = (Button) findViewById(R.id.button2);
        bt3 = (Button) findViewById(R.id.button3);
        bt4 = (Button) findViewById(R.id.button4);

        newHeight = et2.getHeight();
        tv.setHeight(newHeight);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                enter1 = Integer.parseInt(et1.getText().toString());
                enter2 = Integer.parseInt(et2.getText().toString());

                int answer = enter1 + enter2;
                tv.setText(String.valueOf(answer));

            }
        });

        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                enter1 = Integer.parseInt(et1.getText().toString());
                enter2 = Integer.parseInt(et2.getText().toString());

                int answer = enter1 - enter2;
                tv.setText(String.valueOf(answer));

            }
        });

        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                enter1 = Integer.parseInt(et1.getText().toString());
                enter2 = Integer.parseInt(et2.getText().toString());

                int answer = enter1 * enter2;
                tv.setText(String.valueOf(answer));

            }
        });

        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                enter1 = Integer.parseInt(et1.getText().toString());
                enter2 = Integer.parseInt(et2.getText().toString());

                int answer = enter1 / enter2;
                tv.setText(String.valueOf(answer));

            }
        });

    }
} 

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter two numbers below and perform an operation to see the output"
            android:gravity="center_horizontal"
            android:textSize="20sp" />

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Enter first  number here -->"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/edittext1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:inputType="numberSigned"/>

        </LinearLayout>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Enter second  number here -->"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/edittext2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:inputType="numberSigned"/>

        </LinearLayout>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Answer -->"
                android:textSize="18sp"
                android:gravity="center_vertical|center_horizontal" />

            <TextView
                android:id="@+id/tvans"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal" />

        </LinearLayout>

    </LinearLayout>

    <Button
        android:text="+"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="-"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button"
        android:layout_centerHorizontal="true"
        android:id="@+id/button2" />

    <Button
        android:text="x"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_alignEnd="@+id/button"
        android:layout_marginTop="39dp"
        android:id="@+id/button3" />

    <Button
        android:text="/"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button3"
        android:layout_alignStart="@+id/button2"
        android:id="@+id/button4" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

当你获得et2的高度时,它没有文字,所以它的高度是0.然后,你为电视设置高度0,你在屏幕上看不到它有文字。

答案 1 :(得分:0)

Try to run the code giving a constant height and width to answerTextView,tvans in your code(for eg: 30 DP height and 30 DP width).
This issue is not related to button. This is due to the invisibility of textView due to 0 height and width

<TextView
            android:id="@+id/tvans"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:gravity="center_horizontal"/>

答案 2 :(得分:-1)

我删除了高度变量newHeight以及与之关联的所有内容。