застрял на создании слушателя - PullRequest
0 голосов
/ 27 февраля 2012

я полностью застрял ... моя цель - реализовать .. простое действие типа "слушателя", но я не знаю как ... вот мой код

// 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 голосов
/ 29 февраля 2012

вот ответ .. это довольно просто .. мой ум сошел с ума ..

// 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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...