0

カスタムビューを1つ用意しました。アクティビティの1つに追加されます。アクティビティの内容を変更したいことに応じて、そのビューで発生するイベントに追加されます。カスタムビュークラスとアクティビティクラスが異なるため、私はそれを処理できません。これについて私を助けてください。事前に感謝します。

4

2 に答える 2

0

メイン アクティビティから 2 つのカスタム ビューとカスタム リスナーを使用してアプリケーションを構築するには、次のようにします。

インターネット上やSO全体で多くの同様の投稿があったことは知っていますが、これはシンプルで完全です。

あなたの活動クラス:

public class MyActivity extends Activity{
  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //initializing custom views
    MyCustomView1 myCustomView1 = new MyCustomView1(parameterList);
    MyCustomView2 myCustomView2 = new MyCustomView2(parameterList);

    //adding both custom views to the main activity
    mainView.addView(myCustomView1);
    mainView.addView(myCustomView1);

    //adding custom listener to the custom view 1
    myCustomView1.setCustomEventListener(new OnCustomEventListener() {

        @Override
        public void onEvent() {
            //firing an event of custom view 1
            Toast.makeText(MainActivity.this, "Touched custom view 1",
                    Toast.LENGTH_SHORT).show();
        }
    });

    //adding custom listener to the custom view 2
    myCustomView2.setCustomEventListener(new OnCustomEventListener() {

        @Override
        public void onEvent() {
            //firing an event of custom view 2
            Toast.makeText(MainActivity.this, "Touched custom view 2",
                    Toast.LENGTH_SHORT).show();
        }
    });
  }
}

MyCustomView1 クラス:

public class MyCustomView1 extends LinearLayout{
  OnCustomEventListener myCustomEventListener;

  public MyCustomView1(ParameterList){
    super(ContextFromParameterList);
    //Just adding something to the custom view 1 in order to distinguish it on the screen
    TextView tv = new TextView(ContextFromParameterList);
    tv.setText("Hello world from custom view 1");
    addView(tv);

    this.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //delegating one event to another (delegating touch event to custom event)
            if (MyCustomView1.this.myCustomEventListener != null)
                MyCustomView1.this.myCustomEventListener.onEvent();
            return false;
        }
    });
  }

  public void setCustomEventListener(OnCustomEventListener eventListener) {
    //setting custom listener from activity
    myCustomEventListener = eventListener;
  }
}

MyCustomView2 クラス:

public class MyCustomView2 extends LinearLayout {
OnCustomEventListener myCustomEventListener;

public MyCustomView2(ParameterList) {
    super(ContextFromParameterList);
    //Just adding something to the custom view 1 in order to distinguish it on the screen
    TextView tv = new TextView(ContextFromParameterList);
    tv.setText("Hello world from custom view 2");
    addView(tv);

    this.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //delegating one event to another (delegating touch event to custom event)
            if (MyCustomView2.this.myCustomEventListener != null)
                MyCustomView2.this.myCustomEventListener.onEvent();
            return false;
        }
    });
}

  public void setCustomEventListener(OnCustomEventListener eventListener) {
    //setting custom listener from activity
    myCustomEventListener = eventListener;
  }
}

リスナー インターフェース:

public interface OnCustomEventListener{
  //interface defines one method. Can be more and methods may have parameters
  public void onEvent();
}

したがって、ここでのアクティビティのようにトーストを表示する代わりに、1 つのビューを非表示にしたり、別のビューを表示したりするなど、独自の方法でトーストを表示することができます。

于 2013-06-06T18:41:14.513 に答える
0

Main_activity (ビューを含む) のコンテンツを更新する必要がある場合は、次のようにカスタム ビューで Main_activity 変数を作成する必要があります。

private Main_activity activity;

そして、次のようにメインアクティビティで任意の関数update()を呼び出すことができます:

this.activity.update();
于 2013-06-06T12:17:29.203 に答える