Java 如何在一个活动中有多个按钮
我的Android应用程序有很多按钮 我的main.xml布局有三个按钮 我知道如何使用按钮从一个活动转到另一个活动,但我不知道如何在一个活动上有多个按钮,每个按钮启动的活动与另一个不同 范例 Main.xml 按钮1 按钮2 Main2.xml 由按钮1启动 About.xml 由按钮2启动 如何使main.java文件执行此操作Java 如何在一个活动中有多个按钮,java,android,Java,Android,我的Android应用程序有很多按钮 我的main.xml布局有三个按钮 我知道如何使用按钮从一个活动转到另一个活动,但我不知道如何在一个活动上有多个按钮,每个按钮启动的活动与另一个不同 范例 Main.xml 按钮1 按钮2 Main2.xml 由按钮1启动 About.xml 由按钮2启动 如何使main.java文件执行此操作 public class NL extends Activity { public void onCreate(Bundle savedInstan
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从一个活动移动到另一个活动。我们在按钮单击侦听器中编写代码
使用intent从一个活动移动到另一个活动。我们在按钮单击侦听器中编写代码这是一个相当宽泛的问题,所以我的答案可能也同样宽泛 1.)首先要了解的是如何构建布局。你说你已经有了3个按钮的布局。在每个按钮的定义中,您需要分配一个android:id属性。这将允许您稍后从您的活动中连接到该按钮。有关更多信息,请参阅 2.)一旦用android:id定义了3个按钮(为了便于讨论,请使用R.id.1、R.id.2和R.id.3),您就会希望在Activities onCreate方法中将Java对象与这些元素挂钩:
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);
用扩展要启动的活动的类的名称替换要启动的活动类。要了解更多信息[查看意向文档]()这是一个相当宽泛的问题,因此我的答案可能也同样宽泛 1.)首先要了解的是如何构建布局。你说你已经有了3个按钮的布局。在每个按钮的定义中,您需要分配一个android:id属性。这将允许您稍后从您的活动中连接到该按钮。有关更多信息,请参阅 2.)一旦用android:id定义了3个按钮(为了便于讨论,请使用R.id.1、R.id.2和R.id.3),您就会希望在Activities onCreate方法中将Java对象与这些元素挂钩:
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);
用扩展要启动的活动的类的名称替换要启动的活动类。要了解更多信息[查看意图文档]()我知道这一点早就得到了回答,但如果其他人偶然发现了它,我想我会提供另一种方法来完成我自己代码中使用的按钮,即使用onClick和View。onclick在xml文件中完成,方法是转到要使click成为可点击的按钮并添加以下行:
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
我之所以使用它,是因为我发现复制和通过它很容易,因为我有多个页面,每个页面上只有几个按钮。因此,对我来说,这帮助我节省了开发应用程序的时间。我知道这个问题早就得到了解决,但如果其他人偶然发现了它,我想我会提供另一种方法来完成我自己代码中使用的按钮,那就是使用onClick和View。onclick在xml文件中完成,方法是转到要使click成为可点击的按钮并添加以下行:
android:onClick="title"
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 );
}});
}
}
其中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 );
}});
}
}