2017-04-10 52 views
0

我已经建立了一个计算器应用程序,它使用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> 
+0

把你的布局文件在这里 –

+0

@LuizFernandoSalvaterra我已经上传了布局/ XML文件。 – Zid

+0

你在logcat中收到任何错误消息吗? – airowe

回答

-1

我删除了高度可变newHeight,一切都与它有关。

+0

这应该是解决您的问题或编辑原始文章?如果是后者,请将其作为“答案”移除,并使用文章中的编辑按钮进行更新。 – codeMagic

0

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

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"/>