2017-10-23 12 views
-4

私はアンドロイドのnoobで、0-9の数字と加算、減算、乗算、除算などの基本演算子を持つ基本的な電卓を作成しています。私は一度に多くの数字を挿入する方法についてちょっと混乱していますか? 私は表示セクションを表示するためにテキストビューを持っていて、その上にいくつかの数字を表示したいと思います。数値をテキストビューに設定するのは混乱しています。 私は1を押すたびに、私は私のコードでそれを使用して値1を設定します。 edittext.setText("1")基本的な電卓を作成するには?

を問題は、それが一度だけ実行することで、私は1つのボタンを押すたびのTextViewの1以上を追加する方法を理解していないのですか? 問題をどのように解決しますか? それはロジックはあなたが最初のテキストをretriveしてからのEditText

editText.setText(editText.getText() + "0");//digit 0 represents number pressed 

に既に存在している番号にそれを追加し、これはあなたのボタンのクリックリスナの内側に配置されていることである私のデザイン that is my desigb

+1

に設定してください(「1」)'あなたは「あなたは完全にテキストをリセットしている追加していません1 " –

+0

https://www.androidauthority.com/build-a-calculator-app-721910/また、Javaの基本を理解することをお勧めします –

答えて

1

です

0

私は、これらは単なるルックスのために、あなたはそれはのJava部分を確認し、多分その後、独自のXMLを使用する必要がありますされているものを参照するために使用@drawableまたは何か他のものを心配していない、のは、これはXMLであるとしましょう最も重要な

<EditText 
          android:layout_width="wrap_content" 
          android:layout_height="60dp" 
          android:id="@+id/EditText" 
          android:layout_weight="1" 
          android:background="@drawable/no_border" 
          android:inputType="number"/> 

    <RelativeLayout 

       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/relLayout1" 
       android:baselineAligned="false"> 

       <RelativeLayout 
        android:id="@+id/relLayout2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="10dp"> 

        <Button 
         android:id="@+id/one" 
         android:layout_width="40sp" 
         android:layout_height="40sp" 
         android:layout_margin="4dp" 
         android:background="@drawable/number_button" 
         android:text="1" 
         android:textColor="#008074" /> 

        <Button 
         android:id="@+id/two" 
         android:layout_width="40sp" 
         android:layout_height="40sp" 
         android:layout_margin="4dp" 
         android:layout_toRightOf="@+id/one" 
         android:background="@drawable/number_button" 
         android:text="2" 
         android:textColor="#008074" /> 

        <Button 
         android:id="@+id/three" 
         android:layout_width="40sp" 
         android:layout_height="40sp" 
         android:layout_margin="4dp" 
         android:layout_toRightOf="@+id/two" 
         android:background="@drawable/number_button" 
         android:text="3" 
         android:textColor="#008074" /> 

        <!--________________________second row_______________________--> 
        <Button 
         android:id="@+id/four" 
         android:layout_width="40sp" 
         android:layout_height="40sp" 
         android:layout_alignParentLeft="true" 
         android:layout_alignParentStart="true" 
         android:layout_below="@+id/one" 
         android:layout_margin="4dp" 
         android:background="@drawable/number_button" 
         android:text="4" 
         android:textColor="#008074" /> 

        <Button 
         android:id="@+id/five" 
         android:layout_width="40sp" 
         android:layout_height="40sp" 
         android:layout_below="@+id/one" 
         android:layout_margin="4dp" 
         android:layout_toRightOf="@+id/four" 
         android:background="@drawable/number_button" 
         android:text="5" 
         android:textColor="#008074" /> 

        <Button 
         android:id="@+id/six" 
         android:layout_width="40sp" 
         android:layout_height="40sp" 
         android:layout_below="@+id/one" 
         android:layout_margin="4dp" 
         android:layout_toRightOf="@+id/five" 
         android:background="@drawable/number_button" 
         android:text="6" 
         android:textColor="#008074" /> 

        <!--_________________________third row________________________--> 
        <Button 
         android:id="@+id/seven" 
         android:layout_width="40sp" 
         android:layout_height="40sp" 
         android:layout_below="@+id/four" 
         android:layout_margin="4dp" 
         android:background="@drawable/number_button" 
         android:text="7" 
         android:textColor="#008074" /> 

        <Button 
         android:id="@+id/eight" 
         android:layout_width="40sp" 
         android:layout_height="40sp" 
         android:layout_below="@+id/four" 
         android:layout_margin="4dp" 
         android:layout_toRightOf="@+id/seven" 
         android:background="@drawable/number_button" 
         android:text="8" 
         android:textColor="#008074" /> 

        <Button 
         android:id="@+id/nine" 
         android:layout_width="40sp" 
         android:layout_height="40sp" 
         android:layout_below="@+id/four" 
         android:layout_margin="4dp" 
         android:layout_toRightOf="@+id/eight" 
         android:background="@drawable/number_button" 
         android:text="9" 
         android:textColor="#008074" /> 

        <!--______________________last row______________________--> 

、あなたのactivity.javaにあなたが

//ここにあなたがあなたの数字が行くのEditTextを得ていると数字

newNumber = (EditText) findViewById(R.id.EditText); 
    Button one = (Button) findViewById(R.id.one); 
    Button two = (Button) findViewById(R.id.two); 
    Button three = (Button) findViewById(R.id.three); 
    Button four = (Button) findViewById(R.id.four); 
    Button five = (Button) findViewById(R.id.five); 
    Button six = (Button) findViewById(R.id.six); 
    Button seven = (Button) findViewById(R.id.seven); 
    Button eight = (Button) findViewById(R.id.eight); 
    Button nine = (Button) findViewById(R.id.nine); 
    Button zero = (Button) findViewById(R.id.zero); 
必要があります

し、それらを取得した後、あなたのリスナーを定義し、 `edittext.setTextを行う際に、すべての数

View.OnClickListener listener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Button b = (Button) v; 
      newNumber.append(b.getText().toString()); 
     } 
    }; 
    one.setOnClickListener(listener); 
    two.setOnClickListener(listener); 
    three.setOnClickListener(listener); 
    four.setOnClickListener(listener); 
    five.setOnClickListener(listener); 
    six.setOnClickListener(listener); 
    seven.setOnClickListener(listener); 
    eight.setOnClickListener(listener); 
    nine.setOnClickListener(listener); 
    zero.setOnClickListener(listener); 
関連する問題