Using multiple button to start different activitie

2019-09-15 10:24发布

问题:

I have 3 activities. Main activity which is the home screen with two buttons, each button should show up one of the 2 last activity. I tried many methods for multiple buttons both in intent mode and switch mode; but I still can have two working button. The first button starts its linked activity without any problem, but the second button still won't show up. Here is the java code:

package com.live.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    Button button;
    Button button01;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addListenerOnButton();
    }

    public void addListenerOnButton() {

        final Context context = this;

        button = (Button) findViewById(R.id.buttonUrl);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, WoneWideo.class);
                startActivity(intent);   

            }

        });

    }

    public void addListenerOnButton2() {

        final Context context = this;

        button01 = (Button) findViewById(R.id.button01);

        button01.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, WebViewActivity.class);
                startActivity(intent);   

            }

        });

    }

}

The main layout file content:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ..........................
..............................
    <Button
        android:id="@+id/buttonUrl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/live_button" 
         android:onClick="onClick"/>

    <Button
        android:id="@+id/button01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/vod_button" 
         android:onClick="onClick"/>

</LinearLayout>

Also I have all these activities/class declared in the Manifest file.

回答1:

You forget to call addListenerOnButton2(). Anyhow You can make your code simple

Try This:

     Button btn1,btn2;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    btn1 = (Button)findViewById(R.id.buttonUrl);
    btn2 = (Button)findViewById(R.id.button01);

}

public void onClick(View v){

    if(v.getId() == R.id.buttonUrl){
        Intent intent = new Intent(context, WoneWideo.class);
                    startActivity(intent);   

    }else if(v.getId() == R.id.button01){
        Intent intent = new Intent(context, WebViewActivity.class);
            startActivity(intent);  
    }

}


回答2:

When there is a property android:onClick="onClick" in your xml, then there is no need for setting listeners. Only thing you need is, a function with signature public void onClick(View v){}

So, the Activity will be as:

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
public void onClick(View v){

switch(v.getId()){
 case R.id.buttonUrl:
    Intent intent = new Intent(context, WoneWideo.class);
                startActivity(intent);   
break;
case R.id.button01:
    Intent intent = new Intent(context, WebViewActivity.class);
        startActivity(intent);  
break;
}
}


回答3:

You did not call addListenerOnButton2() in onCreate(). You only called addListenerOnButton()