Android应用程序 - 根据按下的次数从按钮输入整数

时间:2017-02-07 18:48:56

标签: java android button

我正在使用android studio为Android编写计算器应用程序。我想用4个按钮输入值和功能。然而,我目前正在做的方式是从按钮上写的文本输入。所以对于我的按钮1/2/3,按下1/2/3会传递给textView。

以下是我的MainActivity:

   package com.example.myfirstapp;

import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity  {
    private int[] operatorButtons = {R.id.operators};
    private int[] numericButtons = {R.id.onetwothree, R.id.fourfivesix, R.id.seveneightninezero};
    private boolean  lastNumeric, stateError;
    private TextView txtScreen;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Find the TextView
        this.txtScreen = (TextView) findViewById(R.id.txtScreen);
        // Find and set OnClickListener to numeric buttons
        setNumericOnClickListener();
        // Find and set OnClickListener to operator buttons, equal button and decimal point button
        setOperatorOnClickListener();
    }


    private void setNumericOnClickListener() {
        // Create a common OnClickListener
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Just append/set the text of clicked button
                Button button = (Button) v;
                if (stateError) {
                    // If current state is Error, replace the error message
                    txtScreen.setText(button.getText());
                    stateError = false;
                } else {
                    // If not, already there is a valid expression so append to it
                    txtScreen.append(button.getText());
                }
                // Set the flag
                lastNumeric = true;
            }
        };
        // Assign the listener to all the numeric buttons
        for (int id : numericButtons) {
            findViewById(id).setOnClickListener(listener);
        }
    }

    private void setOperatorOnClickListener() {
        // Create a common OnClickListener for operators
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // If the current state is Error do not append the operator
                // If the last input is number only, append the operator
                if (lastNumeric && !stateError) {
                    Button button = (Button) v;
                    txtScreen.append(button.getText());
                    lastNumeric = false;

                }
            }
        };
        // Assign the listener to all the operator buttons
        for (int id : operatorButtons) {
            findViewById(id).setOnClickListener(listener);
        }


        // Equal button
        /*findViewById(R.id.btnEqual).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onEqual();
            }
        });*/
    }
}

和我的activity_main:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txtScreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="right|center_vertical"
        android:maxLength="16"
        android:padding="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30sp"
        android:typeface="serif" />
   <!--<Button-->
        <!--android:id="@+id/equal1"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="100dp"-->
        <!--android:text="="-->
         <!--/>-->

    <Button
        android:id="@+id/equal2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="="
        android:layout_alignParentBottom="true"
         />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/txtScreen"
        android:orientation="vertical"
        android:layout_above="@id/equal2">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <Button
                android:id="@+id/onetwothree"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="1/2/3"/>

            <Button
                android:id="@+id/fourfivesix"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="4/5/6"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <Button
                android:id="@+id/seveneightninezero"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="7/8/9/0"/>

            <Button
                android:id="@+id/operators"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="+-*/"/>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

例如,我可以从第一个按钮获得1,2或3的输入吗?所以在1按下你得到1,2按下给2等。

非常感谢任何关于如何向前推进的建议/想法。

亲切的问候, 本

2 个答案:

答案 0 :(得分:1)

您可以使用计时器或延迟变量来检测单击,双击或三击。这个post可能是有意义的。如果时间间隔不是一个因素,您可以只跟踪上次按下的按钮,如果再次按下相同的按钮,则相应地更新文本。

如果您按照方法1进行操作,则按钮onetwothree的点击监听器的代码可能是这样的(我在setNumericOnClickListener()中注释了setOperatorOnClickListener()mainActivity; { {1}}并添加了以下内容:

onCreate

完成MainActivity:

Button onetwothree = (Button) findViewById(R.id.onetwothree);
    onetwothree.setOnTouchListener(new View.OnTouchListener() {

        Handler handler = new Handler();

        int numberOfTaps = 0;
        long lastTapTimeMs = 0;
        long touchDownMs = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touchDownMs = System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_UP:
                    handler.removeCallbacksAndMessages(null);
                    if ((System.currentTimeMillis() - touchDownMs) > ViewConfiguration.getTapTimeout()) {
                        //it was not a tap

                        numberOfTaps = 0;
                        lastTapTimeMs = 0;
                        break;
                    }

                    if (numberOfTaps > 0
                            && (System.currentTimeMillis() - lastTapTimeMs) < ViewConfiguration.getDoubleTapTimeout()) {
                        numberOfTaps += 1;
                    } else {
                        numberOfTaps = 1;
                    }
                    lastTapTimeMs = System.currentTimeMillis();

                    if (numberOfTaps == 1) {
                        handler.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                if (txtScreen.getText().toString() == "") {
                                    txtScreen.setText("1");
                                } else txtScreen.append("1");
                            }
                        }, ViewConfiguration.getDoubleTapTimeout());



                    }else if (numberOfTaps == 2) {
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (txtScreen.getText().toString() == "") {
                                    txtScreen.setText("2");
                                } else txtScreen.append("2");
                            }
                        }, ViewConfiguration.getDoubleTapTimeout());

                    } else if (numberOfTaps == 3) {
                                if (txtScreen.getText().toString() == "") {
                                    txtScreen.setText("3");
                                } else txtScreen.append("3");
                    }
            }

            return true;
        }
    });

答案 1 :(得分:0)

您可以在执行某些操作时获取所有数字

(Button) plusBtn = (Button) findViewById(R.id.plusBtn);
plusBtn.setOnClickListener(new OnClickListener(){
@Override
public voidonClick(View v){
number1 = Integer.parseInt(txtScreen.getText().toString());
});

number1是全局int。但我不知道它对你有什么帮助,如果这是一个好方法。您可以找到更好的解决方案,只需记住如何将Text从TextView解析为Integer以进行计算。