在Android应用程序中,当单击另一个活动中的按钮时,如何启动一个新活动(GUI),以及如何在这两个活动之间传递数据?
当前回答
//在Kotlin中,你可以这样做 /*在第一个活动中,让在活动布局中有一个id为按钮的按钮。 假设我必须将数据作为String类型从一个活动传递到另一个*/
val btn = findViewById<Button>(R.id.button)
btn.setOnClickListener {
val intent = Intent(baseContext, SecondActivity::class.java).apply {
putExtra("KEY", data)
}
startActivity(intent)
}
//在第二个活动中,你可以从另一个活动中获取数据
val name = intent.getStringExtra("KEY")
/*假设你必须传递一个自定义对象,那么它应该是Parcelable。 让有类拼贴类型,我必须从一个活动传递到另一个 * /
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
class Collage(val name: String, val mobile: String, val email: String) : Parcelable
首先,让这里的数据是拼贴类型。我必须把它交给另一个活动。* /
val btn = findViewById<Button>(R.id.button)
btn.setOnClickListener {
val intent = Intent(baseContext, SecondActivity::class.java).apply {
putExtra("KEY", data)
}
startActivity(intent)
}
//那么从第二个Activity中我们将得到as
val item = intent.extras?.getParcelable<Collage>("KEY")
其他回答
点击按钮打开活动的最简单的方法是:
在res文件夹下创建两个活动,在第一个活动中添加一个按钮,并为onclick函数命名。 每个活动应该有两个java文件。 下面是代码:
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goToAnotherActivity(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
SecondActivity.java
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
}
}
AndroidManifest.xml(只需将此代码块添加到现有代码中)
</activity>
<activity android:name=".SecondActivity">
</activity>
在发送活动中尝试以下代码
//EXTRA_MESSAGE is our key and it's value is 'packagename.MESSAGE'
public static final String EXTRA_MESSAGE = "packageName.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
....
//Here we declare our send button
Button sendButton = (Button) findViewById(R.id.send_button);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//declare our intent object which takes two parameters, the context and the new activity name
// the name of the receiving activity is declared in the Intent Constructor
Intent intent = new Intent(getApplicationContext(), NameOfReceivingActivity.class);
String sendMessage = "hello world"
//put the text inside the intent and send it to another Activity
intent.putExtra(EXTRA_MESSAGE, sendMessage);
//start the activity
startActivity(intent);
}
从接收活动尝试以下代码:
protected void onCreate(Bundle savedInstanceState) {
//use the getIntent()method to receive the data from another activity
Intent intent = getIntent();
//extract the string, with the getStringExtra method
String message = intent.getStringExtra(NewActivityName.EXTRA_MESSAGE);
然后将以下代码添加到AndroidManifest.xml文件中
android:name="packagename.NameOfTheReceivingActivity"
android:label="Title of the Activity"
android:parentActivityName="packagename.NameOfSendingActivity"
你可以试试下面的代码:
Intent myIntent = new Intent();
FirstActivity.this.SecondActivity(myIntent);
Intent iinent= new Intent(Homeactivity.this,secondactivity.class);
startActivity(iinent);
目前的回答很好,但初学者需要一个更全面的回答。在Android中有3种不同的方式来启动一个新活动,它们都使用Intent类;意图| Android开发者。
使用按钮的onClick属性。(初学者) 通过匿名类分配OnClickListener()。(中间) 活动范围接口方法使用switch语句。(不是“Pro”)
如果你想继续,这里是我示例的链接:
使用按钮的onClick属性。(初学者)
按钮有一个onClick属性,可以在.xml文件中找到:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToAnActivity"
android:text="to an activity" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToAnotherActivity"
android:text="to another activity" />
在Java类中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
public void goToAnActivity(View view) {
Intent intent = new Intent(this, AnActivity.class);
startActivity(intent);
}
public void goToAnotherActivity(View view) {
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);
}
优点:易于动态制作,模块化,并且可以轻松地将多个onclick设置为相同的意图。
缺点:回顾时可读性较差。
通过匿名类分配OnClickListener()。(中间)
这是当你为每个按钮设置一个单独的setOnClickListener()并用自己的意图覆盖每个onClick()时。
在Java类中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), AnActivity.class);
view.getContext().startActivity(intent);}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), AnotherActivity.class);
view.getContext().startActivity(intent);}
});
优点:容易在飞行中制作。
缺点:会有很多匿名类,这将使可读性在审查时变得困难。
活动范围接口方法使用switch语句。(不是“Pro”)
这是当您在onClick()方法中为按钮使用switch语句来管理所有Activity的按钮时。
在Java类中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
Intent intent1 = new Intent(this, AnActivity.class);
startActivity(intent1);
break;
case R.id.button2:
Intent intent2 = new Intent(this, AnotherActivity.class);
startActivity(intent2);
break;
default:
break;
}
优点:易于按钮管理,因为所有按钮意图都注册在一个onClick()方法中
关于问题的第二部分,传递数据,请参阅如何在Android应用程序的活动之间传递数据?
编辑:不——“专业”
推荐文章
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的