Bundl类是为字符串与某组件对象建立映射关系的组件,它与Intent配合使用,可在不同的Activity之间传送数据。常用方法如下:
1、putString:把字符串用键值对的形式放到Bundle对象中。
2、remove:移除指定key的值。
3、getString:获取指定key的值。
(1)在界面的Activity对象和Bundle对象。
Intent intent=new Intent();
Bundle bundle=new Bundle();
(2)为Intent指定要跳转的界面,并用Bundle存放键值对数据。
Intent.setClass(MainActivity.thiw.secondActivity.class);
bundle.putString("标记1","要传送的信息内容");
(1)从Intent中获取Bundle对象。
Bundle bb=this.getIntent().getString().getExras();
(2)从Bundle对象中按键值对的键名获取对应的数据值。
String str=rb.getString("标记1");
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- tools:context=".MainActivity">-->
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是MainActivity"
android:textSize="36sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="72dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.074" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到SecondActivity"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="134dp" />
<EditText
android:id="@+id/edit"
android:layout_width="220dp"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:text="请输入一个词语"
android:textSize="24sp"
android:layout_marginTop="41dp"
app:layout_constraintTop_toBottomOf="@+id/txt2"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- tools:context=".MainActivity">-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="欢迎来到 SecondActivity"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2" />
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.3" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="248dp"
android:layout_marginLeft="8dp"
android:text="返回第一个页面"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.478"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
Button btn;
EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.edit);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
}
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("edit", edit.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
package com.example.myapplication2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity implements View. OnClickListener{
Button btn2;
TextView txt2;
@Override
public void onCreate(Bundle bunde) {
super.onCreate(bunde);
setContentView(R.layout.activity_second);
txt2=(TextView)findViewById(R.id.txt2);
btn2 = (Button)findViewById(R.id.btn2);
Bundle bb = this.getIntent().getExtras();
String str = bb.getString("edit");
txt2.setText(str);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent2 = new Intent();
intent2.setClass(this,MainActivity.class);
startActivityForResult(intent2,0);
}
}
前言 项目开发中不管是前台还是后台都会遇到烦人的null,数据库表中字段允许空值...
本文实例讲述了AJAX+Servlet实现的数据处理显示功能。分享给大家供大家参考,具...
来源:DeepenStudy 漏洞文件:js.asp % Dimoblog setoblog=newclass_sys oblog.a...
在Flash Player 10.1及以上版本中,adobe新增了全局错误处理程序UncaughtErrorEv...
CKeditor,以前叫FCKeditor,已经使用过好多年了,功能自然没的说。最近升级到3....
本文转载自微信公众号「SH的全栈笔记」,作者SH。转载本文请联系SH的全栈笔记公...
idea官方推送了2020.2.4版本的更新,那么大家最关心的问题来了,之前激活idea202...
大家好,我是狂聊君。 今天来聊一聊 Mysql 缓存池原理。 提纲附上,话不多说,直...
本文转载自微信公众号「SQL数据库」,作者丶平凡世界 。转载本文请联系开发公众...
问题:我们在做flex的开发中,如果用到别人搭建好的框架,而别人的server名称往...