【问题标题】:stuck on listener creation停留在侦听器创建上
【发布时间】:2012-03-17 05:08:18
【问题描述】:

我完全卡住了.. 我的目标是实现一个..简单的“听众”之类的动作 但我不知道如何.. 这是我的代码

// 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
  }
  });
}

我的问题是,我想在 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(){

} );

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

【问题讨论】:

  • 你们为什么给我负面评价? ..我不知道如何解决它..一些提示会很有用..
  • 您在 Android 中使用 Swing(?) 组件。这显示了对 Android 工作原理的基本理解。我建议你学习一些Android开发的基础课程/教程/文章。更不用说你的问题非常模糊。
  • 没有剪断的第一个代码显示我如何在 swing 中执行此操作,但我不知道如何在 andrpid / 或一般情况下在 swing 上执行此操作

标签: java android android-widget


【解决方案1】:

这就是答案..它很简单..我的大脑一片空白..

// 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2021-07-05
    • 2021-12-18
    相关资源
    最近更新 更多