使用多个按钮启动不同的活动 Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20382521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:46:40  来源:igfitidea点击:

Using multiple button to start different activities Android

androidandroid-intent

提问by Ilimaan

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:

我有3个活动。主要活动是带有两个按钮的主屏幕,每个按钮应显示最后两个活动之一。我在意图模式和切换模式下尝试了多种按钮的多种方法;但我仍然可以有两个工作按钮。第一个按钮开始其链接的活动没有任何问题,但第二个按钮仍然不会显示。这是Java代码:

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.

此外,我在清单文件中声明了所有这些活动/类。

回答by Subramanian Ramsundaram

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

你忘记打电话了addListenerOnButton2()。无论如何你可以让你的代码简单

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

}

回答by Nizam

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

android:onClick="onClick"您的 xml 中有一个属性时,则无需设置 listeners。你唯一需要的是,一个带有签名的函数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;
}
}

回答by Siu

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

你没有打addListenerOnButton2()进来onCreate()。你只叫addListenerOnButton()