http://stackoverflow.com/questions/7938516/continuously-increase-integer-value-as-the-button-is-pressed
為了怕被移除,先抄個備份吧:
============================================
private boolean mAutoIncrement = false;
private boolean mAutoDecrement = false;
public int mValue;
private Handler repeatUpdateHandler = new Handler();
EditText txtQty;
class RptUpdater implements Runnable {
public void run() {
if( mAutoIncrement ){
increment();
repeatUpdateHandler.postDelayed( new RptUpdater(), 100 );
} else if( mAutoDecrement ){
decrement();
repeatUpdateHandler.postDelayed( new RptUpdater(), 100 );
}
}
}
public void decrement(){
if(mValue==0)return;
mValue--;
txtQty.setText( ""+mValue );
}
public void increment(){
mValue++;
txtQty.setText( ""+mValue );
}
@Override protected void onCreate(Bundle savedInstanceState) {
...
...
....
txtQty=(EditText)findViewById(id.txtProdQtyInDialog);
mValue=prod2Proc.orderQty;
txtQty.setText(""+mValue);
Button btnQtyAdd=(Button)findViewById(id.btnQtyPlus);
Button btnQtyMinus=(Button)findViewById(id.btnQtyMinus);
btnQtyAdd.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(View arg0) {
increment();
}
}
);
btnQtyAdd.setOnLongClickListener(
new View.OnLongClickListener(){
public boolean onLongClick(View arg0) {
mAutoIncrement = true;
repeatUpdateHandler.post( new RptUpdater() );
return false;
}
}
);
btnQtyAdd.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;
}
});
btnQtyMinus.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(View arg0) {
decrement();
}
}
);
btnQtyMinus.setOnLongClickListener(
new View.OnLongClickListener(){
public boolean onLongClick(View arg0) {
mAutoDecrement = true;
repeatUpdateHandler.post( new RptUpdater() );
return false;
}
}
);
btnQtyMinus.setOnTouchListener( new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL)
&& mAutoDecrement ){
mAutoDecrement = false;
}
return false;
}
});
...
...
...
==============================================================
以上,加油
沒有留言:
張貼留言