安卓开发之传递简单数据(其中两种方法)

 时间:2026-02-12 04:06:29

1、传递简单数据

(思路:先在一个Activity新建一个Intent 用putExtra传输数据【通过一个btn将数据传输过第二个Activity,用组件TextView通过getIntent方法接受intent,然后用getStringExtra接受 】)

2、Main.class的代码部分(一个Intent 用putExtra传输数据)

Button btn  = (Button) findViewById(R.id.btnStartAty);btn.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        Intent i = new Intent(MainActivity.this,TheAty.class);//那个this对应 那个class        i.putExtra("data","这个是第一个Activity的数据");        startActivity(i);    }});

3、ActivityMain.xml文件(一个btn而已)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="启动另一个Activity"        android:id="@+id/btnStartAty"/></LinearLayout>

4、Theacty.xml部分(一个有id的TextView)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/tv"/></LinearLayout>

5、Theaty.java代码部分(getIntent、 找到设置好的tv 用setText方法 调用i.getStringExtra方法)

private TextView tv ;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_the_aty);    Intent i = getIntent();    tv = (TextView) findViewById(R.id.tv);    tv.setText(i.getStringExtra("data"));}

6、传递数据包 Bundle

Main.class 代码部分(main.xml部分还是只有一个带id的btn)

public void onClick(View v) {    Intent i = new Intent(MainActivity.this,theActy.class);    Bundle b = new Bundle();    b.putString("name","hhg");    b.putInt("age",14);    i.putExtras(b);    startActivity(i);

7、另一个Activity.class代码部分(另一个Activity.xml部分还是只有一个带id的textView)

private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_the_acty);    Intent i = getIntent();    Bundle data= i.getExtras();    tv = (TextView) findViewById(R.id.tv);    tv.setText(String.format("name=%s,age=%d,name1=%s",data.getString("name"),data.getInt("age"),data.getString("name1","它是默认值")));

8、(转换说明符

      %a(%A)     浮点数、十六进制数字和p-(P-)记数法(C99)

      %c             字符

      %d             有符号十进制整数

      %f              浮点数(包括float和doulbe)

      %e(%E)     浮点数指数输出[e-(E-)记数法]

      %g(%G)     浮点数不显无意义的零"0"

      %i              有符号十进制整数(与%d相同)

      %u             无符号十进制整数

      %o             八进制整数    e.g.     0123

      %x(%X)      十六进制整数0f(0F)   e.g.   0x1234

      %p             指针

      %s             字符串)

  • 小缇娜的奇幻之地怎么调中文
  • taptap怎么修改昵称
  • 怎么算0.9+99x0.9更简便?
  • 天津到桂林旅游攻略
  • 扎伤的应急处理
  • 热门搜索
    无土养殖泥鳅 口腔溃疡是缺什么维生素 harry是什么意思 油然而生是什么意思 维生素e软胶囊涂脸 pu什么意思 自渎是什么意思 技校是什么学历 副市长是什么级别 林蛙养殖技术