2017-04-10 3 views
0

두 개의 값을 삽입 할 수있는 EditText를 사용하는 계산기 앱을 작성한 다음 조작이있는 4 개의 버튼 (예 : +, -, /, x) . 버튼에 할당 된 기능을 수행하지 않는 것처럼 보이지만 TextView에 값이 전송되지 않습니다. 그 버튼들을 조작하는 것을 도와 주실 수 있겠습니까? 내가 높이와 연관된 변수 newHeight 모든 것을 제거android - 계산기 앱에서 버튼이 작동하지 않습니다.

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 Layout/XML 파일을 업로드했습니다. – Zid

+0

logcat에 오류 메시지가 표시됩니까? – airowe

답변

-1

:

여기 아래에있는 내 코드입니다.

+0

문제의 해결 방법이거나 원래 게시물의 수정 내용일까요? 후자의 경우 '답변'으로 삭제하고 게시물의 수정 버튼을 사용하여 업데이트하십시오. – codeMagic

0

et2의 getHeight를 입력하면 텍스트가 표시되지 않으므로 높이가 0입니다. 그런 다음 TV의 높이를 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"/> 
관련 문제