如何使用同一屏幕中的两个不同按钮启动两个不同的活动?

时间:2014-09-13 16:43:27

标签: java android eclipse android-activity

我有 如果有人知道请帮助将是非常有用的.. 谢谢。问题是,当我点击我想要的按钮时,它出现在我的第一个列表中。当我点击第二个按钮时,它什么也没有显示 第三项活动:

<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="ca.my.demo.buttondemo.ThirdActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Gala Olympos" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="20dp"
        android:text="Gala Delta" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="23dp"
        android:text="Krema galaktos" />

</RelativeLayout>


Second activity:
<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="ca.my.demo.buttondemo.SecondActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mprizoles Xoirines" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"
        android:text="Mprizoles Mosxarisies" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="18dp"
        android:text="Kotopoula" />

</RelativeLayout>


Main Activity:

package ca.my.demo.buttondemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
    private final String TAG = "DemoButtonApp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupMessageButton();

    } 


    private void setupMessageButton() {

            Button messageButton = (Button)  findViewById(R.id.btnDisplayMessage);


             messageButton.setOnClickListener(new View.OnClickListener() 


         {

            @Override
        public void onClick(View v) {
            Log.i(TAG, "You clicked the button");


            startActivity(new Intent(MainActivity.this, SecondActivity.class));


            }


     }  
         ) ;
         }



    }

1 个答案:

答案 0 :(得分:0)

解决方案是在每个按钮的java代码中都有一个onClick实现

例如:

Button btn1 = (Button) row.findViewById(R.id.btn1);
Button btn2 = (Button) row.findViewById(R.id.btn2);

btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getContext(), second.class);
                getContext().startActivity(i);
            }
       });

btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getContext(), third.class);
                getContext().startActivity(i);
            }
       });