从班级

时间:2017-10-03 11:50:19

标签: java android

我为侦听器创建了一个类,用于活动中的已重新按钮。但是在活动中我有int值而不是需要递增/递减。

在课堂上,我每500ms增加一次值,并在活动中更改textview,但不能更改活动中的值。

我在活动中有这个代码:

int time = 10;
TextView timeText;
Button increaseButton;

rb.addButton(increaseButton,timeText,time,true);

我传递一个按钮(视图),textview,time(int),如果需要增加或减少。在课堂上我得到时间,并增加它,我如何自动改变活动中的时间值?

编辑:在活动中,我有一个按钮,当按下它时,在课堂上(rb,在其他文件中),如果它被释放我得到,并且每500ms增加+1时间。并在textView中设置新时间。但是我需要改变活动中的时间int,而不仅仅是在课堂上。

类别:

public class releaseButton {

    private Handler repeatUpdateHandler = new Handler();
    private boolean mAutoIncrement = false;
    private boolean mAutoDecrement = false;

    public int mValue;
    public TextView text;
    public int REP_DELAY = 500;


public void addButton(View button, TextView t, int value, final boolean increment){

    text = t;
    mValue = value;

    button.setOnLongClickListener(
            new View.OnLongClickListener(){
                public boolean onLongClick(View arg0) {
                    if (increment)
                        mAutoIncrement = true;
                    else
                        mAutoDecrement = true;

                    repeatUpdateHandler.post( new RptUpdater() );
                    return false;
                }
            }
    );

    button.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;
            }
            if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL)
                    && mAutoDecrement ){
                mAutoDecrement = false;
            }
            return 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 );
            }
        }
    }

    private void increment(){
        mValue++;
        text.setText(String.valueOf(mValue)+":00");
    }

    private void decrement(){
        mValue--;
        text.setText(String.valueOf(mValue)+":00");
    }

}

此代码有效,mValue在释放按钮时递增/递减。但我需要把这个值放在课堂上的时间int。当我在活动中按下播放按钮时,我需要更改时间值,通常会得到10。

2 个答案:

答案 0 :(得分:1)

您可以创建一个侦听器,并在值更改时通知调用活动:

public class releaseButton {

    OnValueChangedListener listener;

    ...

    public void addButton(View button, TextView t, int value, final boolean increment, OnValueChangedListener listener) {
        this.listener = listener;
        ...
    }

    ...

    private void increment() {
        mValue++;
        if (listener != null) {
            listener.onValueChanged(mValue);
        }
        // You can move this into the calling Activity's onValueChanged()
        text.setText(String.valueOf(mValue)+":00");

    }

    private void decrement() {
        mValue--;
        if (listener != null) {
            listener.onValueChanged(mValue);
        }
        // You can move this into the calling Activity's onValueChanged()
        text.setText(String.valueOf(mValue)+":00");
    }

    public interface OnValueChangedListener {
        void onValueChanged(int newValue);
    }
}

Activity应该按如下方式实现监听器/回调:

TextView timeText;
...

rb.addButton(increaseButton, timeText, time, true, new releaseButton.OnValueChangedListener() {
        @Override
        public void onValueChanged(int newValue) {
            // Do whatever you want to do in the activity when the value changes.
            timeText.setText(String.valueOf(newValue) + ":00");
        }
    };
);

<强> 建议

您可以从Text t中删除releaseButton.addButton()参数,因为您现在可以通过侦听器将新值作为整数发送到调用活动,因此它可以自行设置其timeText。 / p>

答案 1 :(得分:0)

您无法使用以下代码更新int变量time

rb.addButton(increaseButton,timeText,time,true);

因为在Java中, Primitive Data Typesint在方法中按值传递。

3种更新time变量的方法:

  1. 将活动作为方法中的参数传递
  2. 在您的活动中创建一个公共静态setter。
  3. 创建一个监听器,告诉活动有关更新。
  4. 第一种方式,您可以使用activity作为参数的方法,如下所示:

    public void addButton(View button, TextView t, Activity activity, final boolean increment) {
    
      ...
      activity.time++;
    
    }
    

    第二种方式,您可以创建一个公共静态setter来更新活动中的值:

    public class MyActivity extends Activity {
      private static int mValue;
    
      public static void setValue(int value) {
        mValue = value;
      }
    }
    

    然后您可以在releaseButton中通过以下方式调用它:

    MyActivity.setValue(mValue);
    

    第三种方式,您可以在更新发生时创建一个侦听器。

    首先,在releaseButton类中创建一个接口:

    public class releaseButton {
    
      private ReleaseButtonListener listener;
      public interface ReleaseButtonListener {
        void updateValue(int value);
      }
    
      public void setListener(ReleaseButtonListener listener) {
        this.listener = listener;
      }
    
      public void addButton(View button, TextView t, int value, final boolean increment){
    
        ...
        // when there is a change in the value, tell the listener.
        listener.updateValue(mValue);
      }
    }
    

    然后,要监听更改,您需要在活动中设置监听器:

    rb.addButton(increaseButton,timeText,time,true);
    rb.setListener(new releaseButton ReleaseButtonListener() {
        void updateValue(int value) {
          // use the value
          mValue = value;
        });