首页 > Java > java教程 > 正文

How To Pass Data To Another Activity

Barbara Streisand
发布: 2024-09-22 20:15:32
原创
498 人浏览过

How To Pass Data To Another Activity

there are two activities

  • MainActivity.java

  • SettingActivity.java

MainActivity.java

 public void launchSettings(View v){

        //Launch a new activity

        Intent i = new Intent(this,SettingActivity.class);
        String message = ((EditText)findViewById(R.id.editTextText)).getText().toString();
        i.putExtra("cool", message);
        startActivity(i);
    }
登录后复制

SettingActivity.java

public class SettingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_setting);

        Intent i = getIntent();
        String message = i.getStringExtra("cool");
        TextView t = findViewById(R.id.textview);
        t.setText(message);
    }
}
登录后复制

There are XMl Files

1.activity_main.xl

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editTextText"
        android:layout_width="470dp"
        android:layout_height="64dp"
        android:ems="10"
        android:inputType="text"
        android:text="Name"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="7dp"
        tools:layout_editor_absoluteY="30dp" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="launchSettings"
        android:text="button"
        app:layout_constraintEnd_toEndOf="parent"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteY="125dp" />
</LinearLayout>

登录后复制

2.activitysetting.xml

 <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50sp"
        android:text="hello activity"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.224"
        tools:layout_editor_absoluteX="139dp"
        tools:ignore="MissingConstraints" />
登录后复制

以上是How To Pass Data To Another Activity的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!