Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android应用程序-根据按下的次数从按钮输入整数_Java_Android_Button - Fatal编程技术网

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

Java Android应用程序-根据按下的次数从按钮输入整数,java,android,button,Java,Android,Button,我正在使用android studio为android编写一个计算器应用程序。我想用4个按钮来输入值和函数。然而,我目前的做法是从按钮上的文字输入。因此,对于我的按钮1/2/3,当按下此按钮时,1/2/3将传递到textView 以下是我的主要活动: package com.example.myfirstapp; import android.media.MediaPlayer; import android.support.v7.app.ActionBarActivity; impor

我正在使用android studio为android编写一个计算器应用程序。我想用4个按钮来输入值和函数。然而,我目前的做法是从按钮上的文字输入。因此,对于我的按钮1/2/3,当按下此按钮时,1/2/3将传递到textView

以下是我的主要活动:

   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();
            }
        });*/
    }
}
我的主要活动是:

  <?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,二按得到2,以此类推

非常感谢您对我如何推进这项工作提出的任何建议/想法

亲切问候,,
Ben

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

如果您遵循方法一,则按钮的单击侦听器的代码可能如下所示(我注释了
setNumericOnClickListener()
setOperatorOnClickListener()
;在
main活动
onCreate
中添加了以下内容):

Button onetwother=(Button)findViewById(R.id.onetwother);
setOnTouchListener(新视图.OnTouchListener()){
Handler=newhandler();
int numberOfTaps=0;
长lastTapTimeMs=0;
长接地毫秒=0;
@凌驾
公共布尔onTouch(视图v,运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
touchtownms=System.currentTimeMillis();
打破
case MotionEvent.ACTION\u UP:
handler.removeCallbacksAndMessages(null);
if((System.currentTimeMillis()-touchDownMs)>ViewConfiguration.getTapTimeout()){
//那不是水龙头
点击次数=0;
lastTapTimeMs=0;
打破
}
如果(点击次数>0
&&(System.currentTimeMillis()-lastTapTimeMs)
完成主要活动:

package com.example.myfirstapp;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;
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();

    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 false;
        }
    });
}


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);
    }


}
}
package com.example.myfirstapp;
导入android.os.Bundle;
导入android.os.Handler;
导入android.support.v7.app.ActionBarActivity;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.ViewConfiguration;
导入android.widget.Button;
导入android.widget.TextView;
公共类MainActivity扩展了ActionBarActivity{
私有int[]运算符按钮={R.id.operators};
private int[]numericButtons={R.id.onetwother,R.id.fourfivesix,R.id.seveneightninezero};
私有布尔lastNumeric,stateError;
私有文本视图txtScreen;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找文本视图
this.txtScreen=(TextView)findViewById(R.id.txtScreen);
//查找并将OnClickListener设置为数字按钮
//setNumericOnClickListener();
//查找并设置OnClickListener到运算符按钮、相等按钮和小数点按钮
//setOperatorOnClickListener();
按钮onetwother=(按钮)findViewById(R.id.onetwother);
setOnTouchListener(新视图.OnTouchListener()){
Handler=newhandler();
int numberOfTaps=0;
长lastTapTimeMs=0;
长接地毫秒=0;
@凌驾
公共布尔onTouch(视图v,运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
touchtownms=System.currentTimeMillis();
打破
case MotionEvent.ACTION\u UP:
handler.removeCallbacksAndMessages(null);
if((System.currentTimeMillis()-touchDownMs)>ViewConfiguration.getTapTimeout()){
//那不是水龙头
点击次数=0;
最后一次=
package com.example.myfirstapp;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;
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();

    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 false;
        }
    });
}


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);
    }


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