在Android Studio中创建3个文本字段和一个按钮来显示另一个活动的结果,可以按照以下步骤进行操作:
<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>
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);
}
});
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);
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
完成以上步骤后,你就成功地在Android Studio中创建了3个文本字段和一个按钮来显示另一个活动的结果。当用户在文本字段中输入内容并点击按钮时,将会打开ResultActivity,并显示输入的文本内容。
领取专属 10元无门槛券
手把手带您无忧上云