坚持听众创作

时间:2012-02-27 18:06:01

标签: java android android-widget

我完全陷入困境.. 我的目标是实现一个简单的“监听器”,就像动作一样 但我不知道如何.. 这是我的代码

// normaly i create an action like this
// this is JUST an example how i do this in swing
//
class la extends JFrame implements ..<someAciton>..{

  JButton b = new JButton("");
  add(b);
  b.setAction( new Action(){
  void click(){
  // bla
  }
  });
}

我的问题是,我想在android中一般在java中创建这个功能 目前我有3个班级和1个接口

    // the interface, it holds my action, wich should be performed
    //
    //
    public interface GameActionInterface extends EventListener {
      public void onTouched( MotionEvent event );
    }

// this is the class whichs runs the action
//
//
public class GameActionListener {
    ArrayList<GameLayoutBase>touchedListener = new ArrayList();
    public void addTouchListener(GameLayoutBase  obj){
        touchedListener.add(obj);
    }
    public void onTouched( MotionEvent event){
        for( GameLayoutBase elem : touchedListener ){
            elem.onTouched(event);
        }
    }

}

// this class should to the action later
//
//
public class GameLayoutElement extends GameLayoutBase {

    public GameLayoutElement(String ID) {
        super(ID);

    }
}

// my main class
//
//
gameEventListener = new GameActionListener();

        // creating the obj
        layout_Element_Player[0] = new GameLayoutElement("Builder_countA");

//        
// and here is the PROBLEM - i want to overwrite the current function with my "own" code
        gameEventListener.addTouchListener(layout_Element_Player[0]);

        // adding the element to the listener
        //
        //
        layout_Element_Player[0].onTouched( new GameActionInterface(){

});

我不知道如何解决这个问题:(

1 个答案:

答案 0 :(得分:0)

这里是答案..它非常简单......我的思绪一片空白......

// handels the button
public class TestButtonVerwaltung {

    ArrayList <TestButton>liste  = new ArrayList();

    public void add( TestButton object){
        liste.add(object);
    }

    public void doAction(  MotionEvent event){

        for( TestButton elem : liste ){
            elem.on(event);
        }
    }

}

// new button    
public class TestButton {

    View.OnTouchListener it;

    public void addOnTouched( View.OnTouchListener doit ){
        it = doit;
    }
    public void on(MotionEvent event){
        it.onTouch(null, event);
    }

}

// in the main
TestButton testBt = new TestButton();
TestButtonVerwaltung tBV = new TestButtonVerwaltung();
tBV.add(testBt);
//the trigger function
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        tBV.doAction(event);
}