简体   繁体   English

如何在一个活动中具有多个按钮

[英]How to have multiple buttons in one activity

My Android app having many buttons. 我的Android应用有很多按钮。

My main.xml layout has three buttons. 我的main.xml布局具有三个按钮。

I know how to use buttons to go from one activity to another, But i don't know how to have multiple buttons on one activity, with each launching a different activity than the other. 我知道如何使用按钮从一个活动转到另一个活动,但是我不知道如何在一个活动上具有多个按钮,每个按钮启动的活动与另一个活动不同。

EXAMPLE

Main.xml Main.xml

Button1 Button2 Button1 Button2

Main2.xml Main2.xml

Launched by button1 由button1启动

About.xml About.xml

Launched by Button2 由Button2启动

How do i make the main.java file do this? 我如何使main.java文件做到这一点?

public class NL extends Activity {



     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
          Button b1=(Button)findViewById(R.id.Button01);
          Button b2=(Button)findViewById(R.id.Button02);
          b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent myintent2 = new Intent(NL.this,Button1.class);
                startActivity(myintent2);

            }
        });
          b2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    Intent myintent2 = new Intent(NL.this,Button2.class);
                    startActivity(myintent2);

                }
            }); 
    }
}

move from one activity to another activity using intent.we write that code in button click listener 使用intent从一个活动转移到另一个活动。我们在按钮点击监听器中编写该代码

This is a pretty broad question, so my answer will probably seem equally broad. 这是一个相当广泛的问题,因此我的回答可能看起来也同样广泛。

1.) The first thing to understand is how to build your layout. 1.)首先要了解的是如何构建布局。 You say you have a layout with 3 buttons already. 您说您已经有3个按钮的布局。 Within the definition for each one of those buttons you need to assign an android:id attribute. 在每个按钮的定义中,您需要分配android:id属性。 This is what will allow you to later hook into that button from your Activity. 这就是允许您稍后从“活动”中挂接到该按钮的原因。 For more information see here 有关更多信息,请参见此处

2.) Once you have your 3 buttons defined with android:id's (lets use R.id.1 , R.id.2 and R.id.3 for sake of discussion) you'd want to hook Java objects to these elements in your Activities onCreate method: 2。)一旦您用android:id定义了3个按钮(为了讨论起见,请使用R.id.1,R.id.2和R.id.3),您就要将Java对象钩接到这些元素上在您的Activity onCreate方法中:

Button button3 = (Button) getViewById(R.id.3)

Do this for your 3 buttons. 为您的3个按钮执行此操作。

3.) The next step is to attach an onClick listener to your buttons 3.)下一步是将onClick侦听器附加到您的按钮上

button3.setOnClickListener(new OnClickListener(){
  public void onClick(View v){
    //place code to execute here
  }
});

As with most gui frameworks in java, this mechanism defines the code that is executed when your button is clicked. 与Java中的大多数gui框架一样,此机制定义单击按钮时执行的代码。 If you want to launch a new Activity from this button, you would create an intent object and launch it like: 如果要通过此按钮启动新的“活动”,则可以创建一个意图对象并按如下方式启动它:

Intent intent = new Intent(this, TheActivityClassYouWantToLaunch.class);
startActivity(intent);

Replace TheActivityClassYouWantToLaunch with the name of the class which extends Activity that you want to launch. 用扩展要启动的Activity的类的名称替换TheActivityClassYouWantToLaunch。 To learn more [check out the documentation on Intents] ( http://developer.android.com/reference/android/content/Intent.html ) 要了解更多信息,请[查看有关Intent的文档]( http://developer.android.com/reference/android/content/Intent.html

I know this has long since been answered, but if anyone else stumbles across it, I figured I would provide another way to do buttons that I am using for my own code is by using the onClick and View. 我知道这早就得到了回答,但是如果有人偶然发现它,我想我可以通过onClick和View提供另一种方法来做我自己的代码使用的按钮。 onclick is done in the xml file by going down to the button you want to make click-able and adding the line: 通过单击要使其可单击的按钮并添加以下行,在xml文件中完成onclick:

    android:onClick="title"

Where title is the name of the part of the java activity class file that goes with that xml file. 其中title是与该xml文件一起的Java活动类文件的一部分的名称。 Over in the java file you'll then add into the public class: 在Java文件中,然后将其添加到公共类中:

public void title(View buttonName) {
    // Should take user to the next view when button pressed
    Intent intent1 = new Intent(this, NextClass.class);
    startActivity(intent1);
}

Where buttonName is the name of the button that you added the onClick part to and NextClass is the name of the java file you wish to access. 其中,buttonName是您添加了onClick部件的按钮的名称,NextClass是您要访问的Java文件的名称。 For your examples, in your Main.xml you would go down to button1 and add the onClick code then go over to Main.java and add the public void code, changing title to what ever you want, such as launchMain2. 对于您的示例,在Main.xml中,您将转到button1并添加onClick代码,然后转到Main.java并添加公共无效代码,将标题更改为所需的名称,例如launchMain2。 In that same .java you would change NextClass.class to Main2.class. 在相同的.java中,您将NextClass.class更改为Main2.class。

I use this because I find it easy to just copy and past it since i have multiple pages with just a few buttons on each page of my app. 我之所以使用它,是因为我发现自己有多个页面,并且在应用程序的每个页面上只有几个按钮,因此复制和粘贴起来很容易。 Thus for me, this helped me save time on working on the app. 因此,对我来说,这帮助我节省了开发应用程序的时间。

i use this code
     ImageButton bot1;
 ImageButton bot2;
 ImageButton bot3;
 ImageButton bot4;
 Intent intentbot1; 
 Intent intentbot2;
     Intent intentbot3;
     Intent intentbot4;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.currentactivity);
    bot1= (ImageButton)findViewById(R.id.bot1);
    bot2 = (ImageButton)findViewById(R.id.bot2);
    bot3 = (ImageButton)findViewById(R.id.bot3);
    bot4 = (ImageButton)findViewById(R.id.bot4);


{   final Intent intentbot1 = new Intent();
      intentbot1.setClass(currentactivity.this, targetactivity.class);
    bot1.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
         startActivity(intentbot1);
            }});}
{
     final Intent intentbot2 = new Intent();
      intentbot2 .setClass(currentactivity.this, targetactivity.class);
    bot2.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
         startActivity(intentbot2 );
            }});}

{ final Intent intentbot3 = new Intent();
 intentbot3 .setClass(currentactivity.this, targetactivity.class);
bot3.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
 startActivity(intentbot3 );
       }});
    }
{ final Intent intentbot4 = new Intent();
intentbot4 .setClass(currentactivity.this, targetactivity.class);
bot4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
 startActivity(intentbot4 );
      }});
    }
}

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

相关问题 如何在一个活动中将弹出菜单添加到多个按钮? - how to add pop up menu to multiple buttons in one activity? 如何在第二个活动中打开多个按钮? - How to make multiple buttons open in second activity? 一个活动中可以有多个GoogleApiClients吗? - Is it possible to have multiple GoogleApiClients within one activity? 如何将多个按钮从一个活动添加到Android中的不同活动 - How to add multple buttons from one activity to an a different to activity in android StartActivityForResult 用于多个按钮访问一个活动并在选择时获取不同的数据? - StartActivityForResult for multiple buttons accessing one activity and getting different datas on selection? “标题”视图和按钮:如何在没有自己的Activity的“标题”中将监听器附加到按钮? - “Header” Views and buttons: how do I attach listeners to Buttons in a “header” that does not have its own Activity? 如何在一个活动上随机设置多个背景 - how to setup multiple background on one activity randomly 如何将多个记录从一个活动传递到android中的另一个活动? - How to pass multiple records from one activity to another activity in android? 一项活动中有多个碎片 - Multiple Fragmens in one Activity 多个视频观看一项活动 - Multiple videoview one activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM