2016-11-12 4 views
-1

안드로이드와 나는 간단한 계산기를 만들었습니다. 이제는 textView에서 작업 내역을 보여주고 싶습니다. 나는 그걸하는 법을 모른다. 누군가 나를 도와 줄 수 있을까? 나는 그것이 매우 간단한 프로그래밍이라는 것을 알고 있지만, 나는 자바와 안드로이드 스튜디오에서 일하고있는 im을 시작하기 시작했다. 감사. 편집 : 내가 편집 한 주 코드와 메신저 레이아웃 코드를 추가 할 때 오류가 1 개 발견되었습니다.간단한 계산기의 역사 - 안드로이드

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.GridLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 


public class MainActivity extends AppCompatActivity { 

    Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bplus, bminus, bmulti, bdiv, beq, bc; 

    EditText edit1; 
    TextView hist; 

    int nr1, nr2; 

    boolean plus, minus, multi, div; 
    //List<String> history= new ArrayList<>(); 



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


     b0 = (Button) findViewById(R.id.b0); 
     b1 = (Button) findViewById(R.id.b1); 
     b2 = (Button) findViewById(R.id.b2); 
     b3 = (Button) findViewById(R.id.b3); 
     b4 = (Button) findViewById(R.id.b4); 
     b5 = (Button) findViewById(R.id.b5); 
     b6 = (Button) findViewById(R.id.b6); 
     b7 = (Button) findViewById(R.id.b7); 
     b8 = (Button) findViewById(R.id.b8); 
     b9 = (Button) findViewById(R.id.b9); 

     bplus = (Button) findViewById(R.id.bplus); 
     bminus = (Button) findViewById(R.id.bminus); 
     bmulti = (Button) findViewById(R.id.bmulti); 
     bdiv = (Button) findViewById(R.id.bdiv); 
     bc = (Button) findViewById(R.id.bc); 
     beq = (Button) findViewById(R.id.beq); 

     edit1 = (EditText) findViewById(R.id.edit1); 
     hist = (TextView) findViewById(R.id.hist); 
     //history.get(index); 



     b0.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "0"); 
      } 


     }); 

     b1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "1"); 
      } 

     }); 

     b2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "2"); 
      } 

     }); 

     b3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "3"); 
      } 

     }); 

     b4.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "4"); 
      } 

     }); 

     b4.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "4"); 
      } 

     }); 

     b5.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "5"); 
      } 

     }); 

     b6.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "6"); 
      } 

     }); 

     b7.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "7"); 
      } 

     }); 
     b8.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "8"); 
      } 

     }); 

     b9.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(edit1.getText() + "9"); 
      } 

     }); 

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

       if (edit1 == null){ 
        edit1.setText(""); 
       }else { 
        nr1 = Integer.parseInt(edit1.getText() + ""); 
        plus= true; 
        edit1.setText(null); 
       } 
      } 
     }); 

     bminus.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       nr1 = Integer.parseInt(edit1.getText() + ""); 
       minus = true ; 
       edit1.setText(null); 
      } 
     }); 

     bmulti.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       nr1 = Integer.parseInt(edit1.getText() + ""); 
       multi = true ; 
       edit1.setText(null); 
      } 
     }); 

     bdiv.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       nr1 = Integer.parseInt(edit1.getText()+""); 
       div = true ; 
       edit1.setText(null); 
      } 
     }); 

     bc.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       edit1.setText(""); 
      } 
     }); 


     beq.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       nr2 = Integer.parseInt(edit1.getText() + ""); 

       double res = '0'; 

       if (plus == true){ 
        edit1.setText(nr1 + nr2+""); 
        plus=false; 
       } 


       if (minus == true){ 
        edit1.setText(nr1 - nr2+""); 
        minus=false; 
       } 

       if (multi == true){ 
        edit1.setText(nr1 * nr2+""); 
        multi=false; 
       } 

       if (div == true){ 
        edit1.setText(nr1/nr2+""); 
        div=false; 
       } 


      } 


     }); 

} 

} 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weightSum="1"> 


    <TextView 
     android:layout_width="match_parent" 
     android:id="@+id/hist" 
     android:layout_height="100dp" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/edit1" /> 

    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.10" 
     android:id="@+id/GridLayout"> 

     <Button 
      android:text="5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b5" 
      android:layout_row="1" 
      android:layout_column="1"/> 

     <Button 
      android:text="6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b6" 
      android:layout_row="1" 
      android:layout_column="2"/> 

     <Button 
      android:text="-" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/bminus" /> 

     <Button 
      android:text="7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b7" 
      android:layout_row="2" 
      android:layout_column="0"/> 

     <Button 
      android:text="8" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b8" 
      android:layout_row="2" 
      android:layout_column="1" /> 

     <Button 
      android:text="9" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b9" 
      android:layout_row="2" 
      android:layout_column="2" /> 

     <Button 
      android:text="*" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/bmulti" 
      android:layout_row="2" 
      android:layout_column="3" /> 

     <Button 
      android:text="+" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/bplus" 
      android:layout_row="0" 
      android:layout_column="3" /> 

     <Button 
      android:text="3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b3" 
      android:layout_row="0" 
      android:layout_column="2" /> 

     <Button 
      android:text="4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b4" 
      android:layout_row="1" 
      android:layout_column="0" /> 

     <Button 
      android:text="0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b0" 
      android:layout_row="3" 
      android:layout_column="1" /> 

     <Button 
      android:text="2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b2" 
      android:layout_row="0" 
      android:layout_column="1" /> 

     <Button 
      android:text="1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b1" 
      android:layout_row="0" 
      android:layout_column="0" /> 

     <Button 
      android:text="/" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/bdiv" 
      android:layout_row="3" 
      android:layout_column="3" /> 

     <Button 
      android:text="=" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/beq" 
      android:layout_row="3" 
      android:layout_column="2" /> 

     <Button 
      android:text="C" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/bc" 
      android:layout_row="3" 
      android:layout_column="0" /> 

    </GridLayout> 

</LinearLayout> 

답변

1

먼저 작업에 대한 기본 정보를 저장하는 모델을 만들어야합니다, 당신은리스트 뷰에 어댑터를 통해 추가 할 수 있습니다.

0

arraylist에 저장할 수 있으며 언제든지 하나씩 전화 할 수 있습니다.

List<String> history= new ArrayList<>(); 

는 동일한 버튼 클릭에 역사에 결과를 추가

beq.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      nr2 = Integer.parseInt(edit1.getText() + ""); 

      double res; 

      if (plus == true){ 
       res = nr1 + nr2; 
       edit1.setText(nr1 + nr2+""); 
       plus=false; 
      } 


      if (minus == true){ 
       res = nr1 - nr2; 
       edit1.setText(nr1 - nr2+""); 
       minus=false; 
      } 

      if (multi == true){ 
       res = nr1 * nr2; 
       edit1.setText(nr1 * nr2+""); 
       multi=false; 
      } 

      if (div == true){ 
       res = nr1/nr2; 
       edit1.setText(nr1/nr2+""); 
       div=false; 
      } 

      history.add(String.valueOf(res); 

     } 



    }); 

통화 기록 somowhere을 코드에서 :

history.get(index); //index is integer from 0 to array size 
+0

내가 metod 넣어 입술을 기울인다, 그것은 문자열해야한다. 오류 : (231, 24) 오류 : 추가 (double) 메서드에 적합한 메소드가 없습니다. Collection.add (String)을 사용할 수 없습니다. (인수 불일치, double을 String으로 변환 할 수 없음) 메서드 List.add) 해당 사항 없음 (인수 불일치, double을 String으로 변환 할 수 없음) 내 textView에서 어떻게 표시합니까? TextView 기록; – Paravel

+0

use history.add (String.valueOf (res); – uguboz