2011-01-21 67 views
1

我的Android应用程序有很多按钮。如何在一个活动中有多个按钮

我的main.xml布局有三个按钮。

我知道如何使用按钮从一个活动到另一个活动,但我不知道如何在一个活动上有多个按钮,每个活动都启动不同的活动。

main.xml中

Button1的 Button2的

Main2.xml

推出由button1的

About.xml

通过将Button2

推出我如何让​​main.java文件做到这一点?

回答

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

       } 
      }); 
    } 
} 

举动从一个活动到另一个活动使用intent.we写在按钮的点击收听

+0

完美的作品!非常感谢! – IntelSoftApps 2011-01-21 05:35:13

5

这是一个非常广泛的问题的代码,所以我的答案可能会显得同样广泛。

1.)首先要了解的是如何构建布局。你说你已经有3个按钮的布局。在每个按钮的定义中,您需要指定一个android:id属性。这是什么让你稍后钩住你的活动按钮。欲了解更多信息,请参阅here

2.)一旦你用android:id定义了3个按钮(为了讨论起见,可以使用R.id.1,R.id.2和R.id.3) ð要挂钩的Java对象到您的活动onCreate方法这些元素:

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

这样做对你的3个按键。

3.)下一个步骤是一个onClick听者附加到按钮

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

如同在Java大多数GUI框架,此机构限定被点击的按钮时所执行的代码。如果你想从这个按钮来启动一个新的活动,您将创建一个意图对象,并启动它想:

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

与延伸要发射活动类的名称替换TheActivityClassYouWantToLaunch。要了解更多信息,请查看[意向文档](http://developer.android.com/reference/android/content/Intent.html

1

我知道这个问题早已得到解答,但如果其他人绊倒了它,我想我会提供另一种方法来执行我正在使用的按钮我自己的代码是通过使用onClick和View。的onclick在XML文件由下到你想点击的按钮并添加行来完成:

android:onClick="title" 

如果标题是java活动类文件,该文件与云的一部分的名字xml文件。在在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); 
} 

哪里BUTTONNAME是所添加的的onClick部分按钮的名称和NextClass是java文件的名称要访问。对于你的例子,在你的Main.xml中,你将下载到button1并添加onClick代码,然后转到Main.java并添加公共无效代码,将标题更改为任何你想要的,比如launchMain2。在那个相同的.java中,你可以将NextClass.class改为Main2.class。

我使用这个,因为我发现很容易只复制和过去它,因为我有多个页面,只有我的应用程序的每个页面上的几个按钮。因此,对我来说,这帮助我节省了在应用程序上工作的时间。

0
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); 
     }}); 
    } 
} 
相关问题