0

ビューフリッパーが 1 つあり、3 つのレイアウトが含まれています。レイアウト 1 には 2 つのボタンがあり、1 つは次へ表示され、もう 1 つは別のアクティビティに移動します。2 番目のレイアウトには 3 つのボタンがあります。私が欲しいのは、2番目のレイアウトを制御し、各ボタンのクリックリスナーを作成して、次のショー用に1つを作成し、別のアクティビティに移動する方法を知ることです。

これはxmlです

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="44dp"
                android:src="@drawable/ballchr3" />


            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/imageView1"
                android:layout_toLeftOf="@+id/imageView3"
                android:src="@drawable/ballchr4" />



        </RelativeLayout>


 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="73dp"
                android:layout_toRightOf="@+id/imageView3"
                android:src="@drawable/ballchr3" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/imageView1"
                android:layout_toLeftOf="@+id/imageView3"
                android:src="@drawable/ballchr4" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="128dp"
                android:src="@drawable/ballchr5" />

        </RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/imageView2"
            android:layout_marginTop="76dp"
            android:src="@drawable/ballchr3" />

    </RelativeLayout>        



    </ViewFlipper>

</RelativeLayout>

活動コード

public class MainActivity extends Activity implements OnClickListener {

    ViewFlipper viewflipper;
    ImageView btn, fail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewflipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
        btn = (ImageView)findViewById(R.id.imageView1);
        fail = (ImageView)findViewById(R.id.imageView2);

        btn.setOnClickListener(this);
        fail.setOnClickListener(this);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        switch(arg0.getId()){
        case R.id.imageView1:
            viewflipper.showNext();
            break;

        case R.id.imageView2:
            Intent i = new Intent(MainActivity.this, Fail.class);
            startActivity(i);
            break;
        }
    }
4

1 に答える 1

0

まず、同じxmlファイルでIDを繰り返さないでください。uがimageView1、2、および3を複数回使用したことがわかります。やめてください。

質問に答えるには、アクティビティでより多くのimageViewを宣言し、上記ですでに行ったようにそれぞれのボタンに割り当て、onClickListenersを設定して実装します。同じxml内にあるため、ビューが終了すると、その参照も存在します。

于 2013-02-18T09:12:46.110 に答える