首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在android studio中创建3个文本字段和一个按钮来显示另一个活动的结果?

在Android Studio中创建3个文本字段和一个按钮来显示另一个活动的结果,可以按照以下步骤进行操作:

  1. 打开Android Studio,并创建一个新的项目。
  2. 在项目的布局文件中,添加3个文本字段和一个按钮。可以使用LinearLayout或RelativeLayout等布局容器来组织它们的位置和大小。例如,可以使用以下代码创建一个垂直排列的布局:
代码语言:txt
复制
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入文本1" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入文本2" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入文本3" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示结果" />

</LinearLayout>
  1. 在MainActivity.java文件中,找到onCreate()方法,并在其中添加以下代码:
代码语言:txt
复制
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText editText1 = findViewById(R.id.editText1);
        EditText editText2 = findViewById(R.id.editText2);
        EditText editText3 = findViewById(R.id.editText3);

        String text1 = editText1.getText().toString();
        String text2 = editText2.getText().toString();
        String text3 = editText3.getText().toString();

        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
        intent.putExtra("text1", text1);
        intent.putExtra("text2", text2);
        intent.putExtra("text3", text3);
        startActivity(intent);
    }
});
  1. 创建一个新的活动类ResultActivity.java,并在其中找到onCreate()方法。在该方法中,添加以下代码:
代码语言:txt
复制
TextView textViewResult = findViewById(R.id.textViewResult);

Intent intent = getIntent();
String text1 = intent.getStringExtra("text1");
String text2 = intent.getStringExtra("text2");
String text3 = intent.getStringExtra("text3");

String result = "文本1:" + text1 + "\n文本2:" + text2 + "\n文本3:" + text3;
textViewResult.setText(result);
  1. 在ResultActivity的布局文件中,添加一个TextView来显示结果。例如,可以使用以下代码:
代码语言:txt
复制
<TextView
    android:id="@+id/textViewResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

完成以上步骤后,你就成功地在Android Studio中创建了3个文本字段和一个按钮来显示另一个活动的结果。当用户在文本字段中输入内容并点击按钮时,将会打开ResultActivity,并显示输入的文本内容。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券