0

大家好,我是 android 的初学者,我对访问变量有疑问.. 在我的应用程序中,我必须显示可绘制的上一个和下一个图像.. 它已经完成了.. 但我的问题是,它应该播放视频根据子类中的图像在父类中,所以我需要访问父类中的子类变量....是否可能?任何想法..谢谢!

// Main class
public class TransitionViewExampleActivity extends Activity implements
    OnClickListener {

    ImageButton play;
    private final int ANIMATION_DURATION_MSEC = 1000;

    private Button _leftButton;
    private Button _rightButton;
    private TransitionView _mainView;

    VideoView videoView;
    Context context;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(R.layout.main);

    play = (ImageButton) findViewById(R.id.ib_play_btn);
    play.setOnClickListener(this);

    _leftButton = (Button) findViewById(R.id.buttonLeft);
    _rightButton = (Button) findViewById(R.id.buttonRight);
    _mainView = (TransitionView) findViewById(R.id.mainView);


    _mainView.setVisibility(View.VISIBLE);

}


    public void onClick(View v) {

 videoView = (VideoView) findViewById(R.id.videoView1);


    switch (v.getId()) {
    // TODO Auto-generated method stub

    case R.id.ib_play_btn:



    //  Toast.makeText(this, "Please Login to view video",Toast.LENGTH_SHORT).show();   

        play.setVisibility(View.INVISIBLE);
        _mainView.setVisibility(View.INVISIBLE);
        _leftButton.setVisibility(View.INVISIBLE);
        _rightButton.setVisibility(View.INVISIBLE);




        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);                    
      String path = "android.resource://" + getPackageName() + "/" + R.raw.test1;
      videoView.setVideoURI(Uri.parse(path));
      videoView.start();

        break;


    }

    if (v == _leftButton) {

        // fadeIn.setDuration(ANIMATION_DURATION_MSEC);

        this.overridePendingTransition(R.anim.slide_in_left,
                R.anim.slide_out_right);

        _mainView.changePage(false);
    } else if (v == _rightButton) {
        this.overridePendingTransition(R.anim.slide_in_right,
                R.anim.slide_out_left);

        _mainView.changePage(true);

    }

   }

 }




// Sub class (Child class)


    class TransitionView extends RelativeLayout {



   /** One of the two in-memory art images */
   private ImageView _artView1;
   /** The other of the two in-memory art images */
   private ImageView _artView2;
  /** Length of art view transition animation, in milliseconds */
   private final int ANIMATION_DURATION_MSEC = 1000;
  /** The underlying ImageSwitcher that performs transitions */
   private ImageSwitcher _imageSwitcher;
  /** Index into _imageIds array */
  private int _currentImage = 0;
  /** All available art image resource ids */

private final Integer[] _imageIds = { R.drawable.thuppaki,
        R.drawable.gouravam, R.drawable.splash2, R.drawable.pic04 };

Animation fadeIn, fadeOut;

Context context;


public TransitionView(Context context) {
    super(context);
    customInit(context);
}


private void customInit(Context context) {

    _imageSwitcher = new ImageSwitcher(context);


    _imageSwitcher.setInAnimation(fadeIn);
    _imageSwitcher.setOutAnimation(fadeOut);

    _artView1 = new ImageView(context);
    _artView1.setImageResource(_imageIds[_currentImage]);

    _artView2 = new ImageView(context);
    _artView2.setImageResource(_imageIds[_currentImage + 1]);

    LayoutParams fullScreenLayout = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    _imageSwitcher.addView(_artView1, 0, fullScreenLayout);
    _imageSwitcher.addView(_artView2, 1, fullScreenLayout);
    _imageSwitcher.setDisplayedChild(0);
    addView(_imageSwitcher, fullScreenLayout);
}

/** @see android.view.View#View(Context, AttributeSet) */
public TransitionView(Context context, AttributeSet attrs) {
    super(context, attrs);
    customInit(context);
}

/** @see android.view.View#View(Context, AttributeSet, int) */
public TransitionView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    customInit(context);
}


public void changePage(boolean pageRight) {
    _currentImage = (pageRight) ? (_currentImage + 1) : (_currentImage - 1);
    if (_currentImage < 0) {

        _currentImage = _imageIds.length - 1;
    } else if (_currentImage >= _imageIds.length) {
        _currentImage = 0;


    }

    if (_imageSwitcher.getCurrentView() == _artView1) {

        _artView2.setImageResource(_imageIds[_currentImage]);
        _imageSwitcher.showNext();
    } else {
        _artView1.setImageResource(_imageIds[_currentImage]);
        _imageSwitcher.showPrevious();
    }
}   

}

最后,我想访问父类中的子类变量_imageIds_currentImage ........知道吗???

4

3 回答 3

1

您始终可以在父类中创建一个使一般工作正常工作的方法,然后在子类中重新定义父类的方法,然后对 and 进行_imageIds操作_currentImage

例子:

class Parent{

    void doSomething(){
        //your general code
    }
}
class Child extends Parent{

    @Override
    void doSomething(){
        super.doSomething(); //calls the parents function

        //add the code where you manipulate the variables
    }
}
于 2013-05-30T10:19:59.467 回答
0

Interface我会用回调方法创建一个,例如onImageSelcted. 像这样的东西:

public interface OnImageSelectedListener(){

     public onImageSelected(String imageName);

}

然后implement在父类中:

public class MyparentClass implements OnImageSelectedListner {

    @Override
    protected void onImageSelected(String imageName){
       playVideo(imageName);
    }

    childClass.setOnImageSelectedListener(this);

}


public class MyChildClass{

    private OnImageSelectedListener listener;

    public setOnImageSelectedListener(OnImageSelectedListener listener){
        this.listener = listener;
    }


    // when the image is selected
    listernet.onImageSelected(imageName);
}
于 2013-05-30T10:33:19.193 回答
0

没有办法做到这一点。您可以创建多个子类的对象,但不清楚将访问哪个对象的成员。

只有在成员是静态的情况下才能做到这一点。在这种情况下,您可以作为 ClassName.memberName 访问它们

于 2013-05-30T10:16:17.170 回答