【发布时间】:2011-01-21 04:48:42
【问题描述】:
我的 Android 应用有很多按钮。
我的 main.xml 布局有三个按钮。
我知道如何使用按钮从一个活动转到另一个活动,但我不知道如何在一个活动上设置多个按钮,每个按钮启动一个不同的活动。
示例
Main.xml
按钮1 按钮2
Main2.xml
由 button1 启动
About.xml
由 Button2 启动
如何让 main.java 文件做到这一点?
【问题讨论】:
我的 Android 应用有很多按钮。
我的 main.xml 布局有三个按钮。
我知道如何使用按钮从一个活动转到另一个活动,但我不知道如何在一个活动上设置多个按钮,每个按钮启动一个不同的活动。
示例
Main.xml
按钮1 按钮2
Main2.xml
由 button1 启动
About.xml
由 Button2 启动
如何让 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);
}
});
}
}
使用意图从一个活动移动到另一个活动。我们在按钮单击侦听器中编写该代码
【讨论】:
这是一个相当广泛的问题,所以我的回答可能看起来同样广泛。
1.) 首先要了解的是如何构建布局。您说您已经有一个带有 3 个按钮的布局。在每个按钮的定义中,您需要分配一个 android:id 属性。这将允许您稍后从 Activity 挂接到该按钮。欲了解更多信息,请参阅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 替换为扩展您要启动的 Activity 的类的名称。要了解更多[查看有关 Intents 的文档] (http://developer.android.com/reference/android/content/Intent.html)
【讨论】:
我知道这个问题早已得到解答,但如果其他人偶然发现它,我想我会提供另一种方法来执行我在自己的代码中使用的按钮,即使用 onClick 和 View。 onclick 在 xml 文件中通过向下到要使其可点击的按钮并添加以下行来完成:
android:onClick="title"
其中 title 是与该 xml 文件一起使用的 java 活动类文件部分的名称。在 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 并添加公共 void 代码,将标题更改为您想要的任何内容,例如 launchMain2。在同一个 .java 中,您可以将 NextClass.class 更改为 Main2.class。
我使用它是因为我发现复制和粘贴它很容易,因为我有多个页面,我的应用程序的每个页面上只有几个按钮。因此,对我来说,这帮助我节省了开发应用程序的时间。
【讨论】:
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 );
}});
}
}
【讨论】: