不断增加的整数值的按钮是pressed整数、按钮、pressed

2023-09-05 06:18:07 作者:番茄你个马铃薯

我是新来的Andr​​oid很抱歉,如果这个问题很容易回答的问题。我有两个按钮,减少和增加按钮,并在它们的中部一个TextView其显示值

当我打的减少按钮,在TextView的减少和增加,当我打的增加键,与没有问题的价值,我得到的工作,但问题是该值只增加了/在一个单一的点击减少1 。我正在努力实现的是,我一直preSS按钮(增加按钮为例),价值也不断增加,只有当我松开上升按钮停止。

这可能吗?如果是这样,你能展示如何实施,一些样品code或引用?谢谢!

下面是我的main.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:重力=中心>

    < RelativeLayout的
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =44dp
        机器人:重力=center_horizo​​ntal>

        <按钮机器人:ID =@ + ID /按钮1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentTop =真
            机器人:文本=&放大器; LT; />

        < TextView的机器人:ID =@ + ID / textView1
            机器人:layout_width =50dp
            机器人:layout_height =FILL_PARENT
            机器人:layout_alignBottom =@ + ID /按钮1
            机器人:layout_toRightOf =@ + ID /按钮1
            机器人:重力=中心
            机器人:文本=45/>

        <按钮机器人:ID =@ + ID /按钮2
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentTop =真
            机器人:layout_toRightOf =@ + ID / textView1
            机器人:文本=&放大器; GT; />

     < / RelativeLayout的>

< / RelativeLayout的>
 
干货推荐 如何设计按钮

和这里是我的Main.java

 进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;

公共类主要扩展活动{

    私人按钮_decrease;
    私人按钮_increase;
    私人TextView的_value;
    私有静态诠释_counter = 45;
    私人字符串_stringVal;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        _decrease =(按钮)findViewById(R.id.button1);
        _increase =(按钮)findViewById(R.id.button2);
        _value =(TextView中)findViewById(R.id.textView1);

        _decrease.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){

                Log.d(SRC,减少的价值......);
                _计数器 - ;
                _stringVal = Integer.toString(_counter);
                _value.setText(_stringVal);
            }
        });

        _increase.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){

                Log.d(SRC,增加价值......);
                _counter ++;
                _stringVal = Integer.toString(_counter);
                _value.setText(_stringVal);
            }
        });

    }
}
 

解决方案

有关的工作,你需要一个线程将更新整数值上的一个按钮,当你长时间preSS。

在您的活动创建一个处理程序:

 专用处理器repeatUpdateHandler =新的处理程序();
 

和2增值经销商将状态:是它增加或减少?只有一组在一个时间

 私人布尔mAutoIncrement = FALSE;
私人布尔mAutoDecrement = FALSE;
 

而present数值

 公众诠释mValue;
 

和,将在另一个线程中运行的类:

 类RptUpdater实现Runnable {
    公共无效的run(){
        如果(mAutoIncrement){
            增量();
            repeatUpdateHandler.postDelayed(新RptUpdater(),REP_DELAY);
        }否则,如果(mAutoDecrement){
            递减();
            repeatUpdateHandler.postDelayed(新RptUpdater(),REP_DELAY);
        }
    }
}
 

添加一个长preSS监听到你的按钮:

  mBTIncrement.setOnLongClickListener(
            新View.OnLongClickListener(){
                公共布尔onLongClick(查看为arg0){
                    mAutoIncrement = TRUE;
                    repeatUpdateHandler.post(新RptUpdater());
                    返回false;
                }
            }
    );

mBTIncrement.setOnTouchListener(新View.OnTouchListener(){
        公共布尔onTouch(视图V,MotionEvent事件){
            如果((event.getAction()== MotionEvent.ACTION_UP || event.getAction()== MotionEvent.ACTION_CANCEL)
                    &功放;&安培; mAutoIncrement){
                mAutoIncrement = FALSE;
            }
            返回false;
        }
    });
 

在上面的情况下的按钮是增量1。创建另一个按钮,将设置mAutoDecrement为true。

和递减()将是一个函数,这将设置这样的实例int变量:

 公共无效递减(){
    mValue--;
    _value.setText(+ mValue);
}
 

您的数字递增的。噢和REP_DELAY是一个静态int变量设置为50。

我看这是杰弗里·科尔的开源NumberPicker可摘录在 http://www.technologichron.net/ 正确的作者归属必须加入。

I'm new to Android so sorry if the question is easy to answer. I have two buttons, a decrease and an increase button, and in the middle of them a TextView which displays a value.

When I hit the decrease button, the value in the TextView decreases and increases when I hit the increase button, no problem with that, I got that working but the problem is the value only increases/decreases by 1 on a single click. What I'm trying to achieve is that as I continuously press the button (the increase button for example), the value is also continuously increasing and only stops when I release the increase button.

Is that possible? If so, can you show some sample code or references on how to implement that? Thanks!

Here is my main.xml

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:gravity="center_horizontal" >

        <Button android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="&lt;" />

        <TextView android:id="@+id/textView1"
            android:layout_width="50dp"
            android:layout_height="fill_parent"
            android:layout_alignBottom="@+id/button1"
            android:layout_toRightOf="@+id/button1"
            android:gravity="center"
            android:text="45" />

        <Button android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/textView1"
            android:text="&gt;" />

     </RelativeLayout>   

</RelativeLayout>

and here is my Main.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {

    private Button _decrease;
    private Button _increase;
    private TextView _value;
    private static int _counter = 45;
    private String _stringVal;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        _decrease = (Button) findViewById(R.id.button1);
        _increase = (Button) findViewById(R.id.button2);
        _value = (TextView) findViewById(R.id.textView1);

        _decrease.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("src", "Decreasing value...");
                _counter--;
                _stringVal = Integer.toString(_counter);
                _value.setText(_stringVal);
            }
        });

        _increase.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("src", "Increasing value...");
                _counter++;
                _stringVal = Integer.toString(_counter);
                _value.setText(_stringVal);
            }
        });

    }
}

解决方案

For that to work, you need a thread that will update the integer value when you long press on a button.

Create a handler in your activity:

private Handler repeatUpdateHandler = new Handler();

And 2 vars which will state: is it increment or decrement? Only one set at a time.

private boolean mAutoIncrement = false;
private boolean mAutoDecrement = false;

And the present number value

public int mValue;

And a class that will run in another thread:

class RptUpdater implements Runnable {
    public void run() {
        if( mAutoIncrement ){
            increment();
            repeatUpdateHandler.postDelayed( new RptUpdater(), REP_DELAY );
        } else if( mAutoDecrement ){
            decrement();
            repeatUpdateHandler.postDelayed( new RptUpdater(), REP_DELAY );
        }
    }
}

Add a long press listener to your button:

mBTIncrement.setOnLongClickListener( 
            new View.OnLongClickListener(){
                public boolean onLongClick(View arg0) {
                    mAutoIncrement = true;
                    repeatUpdateHandler.post( new RptUpdater() );
                    return false;
                }
            }
    );   

mBTIncrement.setOnTouchListener( new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) 
                    && mAutoIncrement ){
                mAutoIncrement = false;
            }
            return false;
        }
    });  

In the above case the button is the increment one. Create another button which will set mAutoDecrement to true.

And decrement() will be a function, which will set your instance int variable like this:

public void decrement(){
    mValue--;
    _value.setText( ""+mValue );
}

You figure the increment out. Oh and REP_DELAY is a static int variable set to 50.

I see this is an excerpt from Jeffrey Cole's open source NumberPicker available at http://www.technologichron.net/ Proper author's attribution must be added.