简体   繁体   English

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

[英]how to launch two different activities with two different buttons which are in the same screen?

i hav If anyone knows please a help will be very useful.. thanks. 我有如果有人知道请帮助将是非常有用的..谢谢。 The problem is that when i click the button i want it appears my first list. 问题是,当我单击按钮时,我希望它出现在我的第一个列表中。 when i click on the second button it appears nothing Third 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));


            }


     }  
         ) ;
         }



    }

The solution is to have an onClick implementation in your java code for each of your buttons 解决方案是在您的Java代码中为每个按钮提供一个onClick实现

Ex: 例如:

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);
            }
       });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM