gpt4 book ai didi

java - Android 应用程序、自定义数字选择器、长按时连续显示 toast

转载 作者:行者123 更新时间:2023-12-02 05:55:43 24 4
gpt4 key购买 nike

您好 stackoverflow 用户,我遇到了一个问题。我在 android、eclipse 中创建了一个自定义数字选择器。我对数字选择器设置了限制,使其不超过 35 且少于 10。问题是,当我连续按下 + 或 - 按钮时,我到达了限制, toast 连续出现在我设置的屏幕上。我设置的时间很短,所以大约出现 4-5 秒,并且 toast 发生变化时有一点 Spark (例如:一个 toast 消失了另一个已经在那里或者现在出现我不知道)

我这里有 longclicklistener 函数:

add.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
mAutoIncrement = true;
repeatUpdateHandler.post(new RptUpdater() );
return false;
}
});
add.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoIncrement )
mAutoIncrement = false;
return false;
}
});
sub.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
mAutoDecrement = true;
repeatUpdateHandler.post(new RptUpdater() );
return false;
}
});
sub.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoDecrement )
mAutoDecrement = false;
return false;
}
});

Toast 的递减和递增函数在这里:

public void increment(){
if(counter<35){
counter++;
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C");
}
else
{
Context context = getApplicationContext();
CharSequence text = "Maximum value is 35°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.TOP, 0, 100);
}
}
public void decrement(){
if(counter>10){
counter--;
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C");
}
else
{
Context context = getApplicationContext();
CharSequence text = "Minimum value is 10°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.BOTTOM, 0, 50);
}

}

如果有人需要查看整个代码,这里是:

package com.example.symbol_temp;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
Integer counter=20;
Button add,sub;



static int REP_DELAY = 50;
public TextView display,showup;
private Handler repeatUpdateHandler = new Handler();
private boolean mAutoIncrement = false;
private boolean mAutoDecrement = false;
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 );
}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

add = (Button) findViewById(R.id.plus);
sub = (Button) findViewById(R.id.minus);
display = (TextView) findViewById(R.id.showtemperature);
showup = (TextView) findViewById(R.id.showmeup);

showup.setText(" "+counter+"°C");
display.setText(""+counter+"°C");
add.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
if(counter<35){
counter++;
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C");
}
else{
Context context = getApplicationContext();
CharSequence text = "Maximum value is 35°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.TOP, 0, 100);
}
}
});

sub.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

if(counter>10){
counter--;
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C");
}
else{
Context context = getApplicationContext();
CharSequence text = "Minimum value is 10°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.BOTTOM, 0, 50);
}
}

});
add.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
mAutoIncrement = true;
repeatUpdateHandler.post(new RptUpdater() );
return false;
}
});
add.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoIncrement )
mAutoIncrement = false;
return false;
}
});
sub.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
mAutoDecrement = true;
repeatUpdateHandler.post(new RptUpdater() );
return false;
}
});
sub.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoDecrement )
mAutoDecrement = false;
return false;
}
});
if(savedInstanceState != null)
counter = savedInstanceState.getInt("myCounter");
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C");
}
public void increment(){
if(counter<35){


counter++;
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C");
}
else
{
Context context = getApplicationContext();
CharSequence text = "Maximum value is 35°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.TOP, 0, 100);
}
}
public void decrement(){
if(counter>10){
counter--;
display.setText( "" + counter+"°C");
showup.setText(" "+counter+"°C");
}
else
{
Context context = getApplicationContext();
CharSequence text = "Minimum value is 10°C!";
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.BOTTOM, 0, 50);
}

}
protected void onSaveInstanceState(Bundle savedInstance) {
super.onSaveInstanceState(savedInstance);
savedInstance.putInt("myCounter",counter);
}

}

我在这里阅读了 stackoverflow 主题:How to Customize Toast In Android但没有帮助我(根据创建项目名称的建议)

最佳答案

只有当数字还不是加法的最大值/减法的最小值时才可以重复:

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

关于java - Android 应用程序、自定义数字选择器、长按时连续显示 toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23102039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com