随着按钮被按下,继续增加整数值 [英] Continuously increase integer value as the button is pressed
问题描述
我是Android的新手,很抱歉,问题很容易回答。我有两个按钮,一个减少和一个增加按钮,在它们的中间一个TextView显示一个值。
当我点击减少按钮,当我点击增加按钮时,TextView减小并增加,没有问题,我得到了工作,但问题是只有一次点击增加/减少1。我想要实现的是,当我不断按下按钮(例如增加按钮)时,值也不断增加,只有当我释放增加按钮时才停止。
$ b $这是可能吗?如果是,您能否显示一些示例代码或参考如何实现?谢谢!
这是我的main.xml
<? xml version =1.0encoding =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>
这里是我的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;
私有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,减少值...);
_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);
}
});
}
}
在您的活动中创建一个处理程序:
p> 私人处理程序repeatUpdateHandler = new Handler();
还有2个vars会声明:是递增还是递减?一次只有一个。
private boolean mAutoIncrement = false;
private boolean mAutoDecrement = false;
现在的数值
public int mValue;
另一个可以在另一个线程中运行的类:
pre>
class RptUpdater实现Runnable {
public void run(){
if(mAutoIncrement){
increment();
repeatUpdateHandler.postDelayed(新的RptUpdater(),REP_DELAY);
} else if(mAutoDecrement){
decrement();
repeatUpdateHandler.postDelayed(新的RptUpdater(),REP_DELAY);
}
}
}
添加一个长按聆听者你的按钮:
mBTIncrement.setOnLongClickListener(
new View.OnLongClickListener(){
public boolean onLongClick查看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)
&& amp; mAutoIncrement){
mAutoIncrement = false;
}
return false;
}
});
在上述情况下,按钮是增量按钮。创建另一个将mAutoDecrement设置为true的按钮。
和reduce()将是一个函数,它将设置您的实例int变量,如下所示:
public void decrement(){
mValue--;
_value.setText(+ mValue);
}
你计算出增量。哦,REP_DELAY是一个静态int变量,设置为50。
我看到这是Jeffrey Cole的开源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="<" />
<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=">" />
</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.
这篇关于随着按钮被按下,继续增加整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!