0

基本的にボタンを押すだけでウェブサイトの動画を再生するAndroidアプリを作っています。以下のコードでわかるように、8つの異なるビデオを再生する8つの異なるボタンがあります。私はプログラミングに不慣れで、これはすべてうまく機能しますが、それを書くための最良の方法ではないことを私は知っています。

より少ないコード行で同じことを行う方法はありますか?

package biz.slwdesign.tvlocallysouthdevon;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;

public class Watch extends Activity implements OnClickListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        setContentView(R.layout.watch); 

        //ImageButton1
        ImageButton video1 = (ImageButton) findViewById(R.id.ImageButton1);
        video1.setOnClickListener(this);

        //ImageButton2
        ImageButton video2 = (ImageButton) findViewById(R.id.ImageButton2);
        video2.setOnClickListener(this);

        //ImageButton3
        ImageButton video3 = (ImageButton) findViewById(R.id.ImageButton3);
        video3.setOnClickListener(this);

        //ImageButton4
        ImageButton video4 = (ImageButton) findViewById(R.id.ImageButton4);
        video4.setOnClickListener(this);

        //ImageButton5
        ImageButton video5 = (ImageButton) findViewById(R.id.ImageButton5);
        video5.setOnClickListener(this);

        //ImageButton6
        ImageButton video6 = (ImageButton) findViewById(R.id.ImageButton6);
        video6.setOnClickListener(this);

        //ImageButton7
        ImageButton video7 = (ImageButton) findViewById(R.id.ImageButton7);
        video7.setOnClickListener(this);

        //ImageButton7
        ImageButton video8 = (ImageButton) findViewById(R.id.ImageButton8);
        video8.setOnClickListener(this);

    }

    public void onClick(View v) {

        if(v.getId() == R.id.ImageButton1){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/austins.mp4");
            videoview1.start();
            videoview1.requestFocus();
            videoview1.setOnCompletionListener(new OnCompletionListener() {           
                public void onCompletion(MediaPlayer videoview) {
                }
            });
        }
        if (v.getId() == R.id.ImageButton2){ 
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/brownsWigs.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if (v.getId() == R.id.ImageButton3){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/frames&Boxes.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if (v.getId() == R.id.ImageButton4){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/hatMckool.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if(v.getId() == R.id.ImageButton5){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/gardenTime.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if (v.getId() == R.id.ImageButton6){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/paulBarclay.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if (v.getId() == R.id.ImageButton7){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/fishShed.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if(v.getId() == R.id.ImageButton8){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/offBoutique.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
    }
}
4

5 に答える 5

2

xmlファイルで、ImageButtonインスタンスを初期化する代わりに、この属性android:onClick="onClick"を設定します

于 2012-04-05T09:28:14.027 に答える
2

いくつかのアドバイスを与える:

1.マップを使用してIDとビデオのURLを保存します。

 //init the video url map in onCreate method
videoMap = new HashMap<Integer, String>();
videoMap.put(R.id.ImageButton1, "http://slwdesign.biz/android/austins.mp4");
...

//then init the onClickListener

Set<Integer> idSets = videoMap.keySet();
for(int id:idSets){
    ImageButton button = (ImageButton)findViewById(id);
    button.setOnClickListener(this);
}

//only init once
videoview1 = (VideoView) findViewById(R.id.videoView1);

また、xmlで、を設定するandroid:onClick="onClick"と、ループのonClickListener初期化をキャンセルできます。

2.onClickメソッド:

 //onclick 
String url = videoMap.get(v.getId());
if(url!=null){
    videoview1.setMediaController(new MediaController(this));
    videoview1.setVideoPath(url);
    videoview1.start();
    videoview1.requestFocus();
}
于 2012-04-05T09:33:47.070 に答える
0

たとえば、次のようにコードを短縮できます。

にリンクする(おそらく)を作成HashMap<Integer, String>します。次に、キーセットをsまで繰り返し 、IDに基づいてパスのルックアップを実行します。staticR.id.*videoPathsetOnClickListeneronClick

編集:その方法について

private static final LinkedHashMap<Integer, String> ID_MAP = new LinkedHashMap<Integer, String>();
static {
    ID_MAP.put(R.id.button1, "http://video1.avi");
    ID_MAP.put(R.id.button2, "http://video2.avi");
    ID_MAP.put(R.id.button3, "http://video3.avi");
    ID_MAP.put(R.id.button4, "http://video4.avi");
    ID_MAP.put(R.id.button5, "http://video5.avi");
}

private final Button[] mButtons = new Button[ID_MAP.size()];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int i = 0;
    for (int id : ID_MAP.keySet()) {
        mButtons[i] = (Button) findViewById(id);
        mButtons[i].setOnClickListener(this);
        i++;
    }
}
@Override
public void onClick(View v) {
    int id = v.getId();
    String url = ID_MAP.get(id);
    // set url etc
}
于 2012-04-05T09:25:48.610 に答える
0

この方法で数行のコードを保存できます。

package biz.slwdesign.tvlocallysouthdevon;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;

public class Watch extends Activity implements OnClickListener {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    setContentView(R.layout.watch);


    //ImageButton1
    ImageButton video1 = (ImageButton) findViewById(R.id.ImageButton1);
    video1.setOnClickListener(this);

    //ImageButton2
    ImageButton video2 = (ImageButton) findViewById(R.id.ImageButton2);
    video2.setOnClickListener(this);

    //ImageButton3
    ImageButton video3 = (ImageButton) findViewById(R.id.ImageButton3);
    video3.setOnClickListener(this);

    //ImageButton4
    ImageButton video4 = (ImageButton) findViewById(R.id.ImageButton4);
    video4.setOnClickListener(this);

    //ImageButton5
    ImageButton video5 = (ImageButton) findViewById(R.id.ImageButton5);
    video5.setOnClickListener(this);

    //ImageButton6
    ImageButton video6 = (ImageButton) findViewById(R.id.ImageButton6);
    video6.setOnClickListener(this);

    //ImageButton7
    ImageButton video7 = (ImageButton) findViewById(R.id.ImageButton7);
    video7.setOnClickListener(this);

    //ImageButton7
    ImageButton video8 = (ImageButton) findViewById(R.id.ImageButton8);
    video8.setOnClickListener(this);

}

public void onClick(View v) {

    setContentView(R.layout.watch);
    VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
    videoview1.setMediaController(new MediaController(this));
    if(v.getId() == R.id.ImageButton1){
    videoview1.setVideoPath("http://slwdesign.biz/android/austins.mp4");
    }
    if (v.getId() == R.id.ImageButton2){ 
    videoview1.setVideoPath("http://slwdesign.biz/android/brownsWigs.mp4");
    }
    if (v.getId() == R.id.ImageButton3){
    videoview1.setVideoPath("http://slwdesign.biz/android/frames&Boxes.mp4");
    }
    if (v.getId() == R.id.ImageButton4){
    videoview1.setVideoPath("http://slwdesign.biz/android/hatMckool.mp4");
    }
    if(v.getId() == R.id.ImageButton5){
    videoview1.setVideoPath("http://slwdesign.biz/android/gardenTime.mp4");
    }
    if (v.getId() == R.id.ImageButton6){
    videoview1.setVideoPath("http://slwdesign.biz/android/paulBarclay.mp4");
    }
    if (v.getId() == R.id.ImageButton7){
    videoview1.setVideoPath("http://slwdesign.biz/android/fishShed.mp4");
    }
    if(v.getId() == R.id.ImageButton8){
    videoview1.setVideoPath("http://slwdesign.biz/android/offBoutique.mp4");
    }
    videoview1.start();
    videoview1.requestFocus();
}

私が何かを見落としていないことを願っています。

于 2012-04-05T09:26:23.027 に答える
0

On Click間違いなく最良の方法は、レイアウトXMLでを定義することです。On Clickこの方法ですべてのボタンのメソッドを作成する必要があります。これは、多くの条件付きコードよりも明確であり、同じコード行を2回以上記述しないでください。メソッドを実行し、必要な場所からメソッドを呼び出します。

ifステートメントの代わりに、コードのタイプに対応するswitch、caseを使用したいと思います。よろしく。

于 2012-08-24T23:37:27.667 に答える