在Android应用程序中,当单击另一个活动中的按钮时,如何启动一个新活动(GUI),以及如何在这两个活动之间传递数据?
当前回答
像下面这样在xml中放置按钮小部件
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
在此之后,初始化和处理点击监听器在活动如下..
在Activity On Create方法中:
Button button =(Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new
Intent(CurrentActivity.this,DesiredActivity.class);
startActivity(intent);
}
});
其他回答
Intent in = new Intent(getApplicationContext(),SecondaryScreen.class);
startActivity(in);
This is an explicit intent to start secondscreen activity.
虽然已经提供了正确的答案,但我在这里搜索语言Kotlin的答案。这个问题不是关于特定于语言的,所以我添加代码来完成这个任务在Kotlin语言。
下面是如何在Kotlin中为andorid做到这一点
testActivityBtn1.setOnClickListener{
val intent = Intent(applicationContext,MainActivity::class.java)
startActivity(intent)
}
在发送活动中尝试以下代码
//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"
在android应用程序中,从另一个活动启动一个活动是非常常见的场景。 要启动一个活动,你需要一个Intent对象。
如何创建意图对象?
意图对象的构造函数中有两个形参
上下文 要启动的活动的名称。(或完整包名)
例子:
例如,如果你有两个活动,说HomeActivity和DetailActivity,你想从HomeActivity启动DetailActivity (HomeActivity—>DetailActivity)。
下面是演示如何启动DetailActivity的代码片段
HomeActivity。
Intent i = new Intent(HomeActivity.this,DetailActivity.class);
startActivity(i);
这样就做完了。
回到按钮点击部分。
Button button = (Button) findViewById(R.id.someid);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(HomeActivity.this,DetailActivity.class);
startActivity(i);
}
});
启动新活动的方法是广播一个意图,您可以使用特定类型的意图将数据从一个活动传递到另一个活动。我的建议是你去看看Android开发者文档中与intent相关的内容;这是一个丰富的信息的主题,并有例子。
推荐文章
- 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
- 按钮背景是透明的