gpt4 book ai didi

java - Android,接受用户的值并在下一个 Activity 中显示它们

转载 作者:行者123 更新时间:2023-11-29 02:42:14 25 4
gpt4 key购买 nike

我应该询问用户姓名、年龄、电子邮件和电话号码,并在另一个 Activity 中显示这些值。 Android 开发培训(来自谷歌)中显示的示例对一个变量(键值对)做了同样的事情。但是,这里我必须传递 4 个值。在查找如何执行此操作时,我遇到了 Bundle。使用相同的方法,第二个 Activity 不显示任何内容,即 Activity 显示空白屏幕,尽管有 4 个 textView 对象。我的代码如下:

MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

public static final String EXTRA_MESSAGE_NAME="com.example.myfirstapp.MESSAGE";
public static final String EXTRA_MESSAGE_AGE="com.example.myfirstapp.MESSAGE";
public static final String EXTRA_MESSAGE_EMAIL="com.example.myfirstapp.MESSAGE";
public static final String EXTRA_MESSAGE_PH="com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Bundle extras=new Bundle();
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
extras.putString(EXTRA_MESSAGE_NAME, message);
EditText editText2 = (EditText) findViewById(R.id.editText7);
message=editText2.getText().toString();
extras.putString(EXTRA_MESSAGE_AGE, message);
EditText editText3 = (EditText) findViewById(R.id.editText5);
message=editText3.getText().toString();
extras.putString(EXTRA_MESSAGE_EMAIL, message);
EditText editText4=(EditText)findViewById(R.id.editText6);
message=editText4.getText().toString();
extras.putString(EXTRA_MESSAGE_PH, message);
intent.putExtras(extras);
startActivity(intent);
}
}

第二个 Activity ,DisplayMessageActivity:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

public class DisplayMessageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);

Intent intent=getIntent();
Bundle extras=intent.getExtras();
String message=extras.getString("EXTRA_MESSAGE_NAME");
String message1=extras.getString("EXTRA_MESSAGE_AGE");
String message2=extras.getString("EXTRA_MESSAGE_EMAIL");
String message3=extras.getString("EXTRA_MESSAGE_PH");

TextView textView= (TextView)findViewById(R.id.textView);
textView.setText(message);

TextView textView2=(TextView)findViewById(R.id.textView2);
textView2.setText(message1);
TextView textView3=(TextView)findViewById(R.id.textView3);
textView3.setText(message2);
TextView textView4=(TextView)findViewById(R.id.textView4);
textView4.setText(message3);
}
}

activity_main.xml:

<EditText
android:id="@+id/editText"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:hint="@string/edit_name"
android:inputType="textPersonName"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/editText6"
app:layout_constraintHorizontal_bias="0.501" />

<EditText
android:id="@+id/editText5"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_email"
android:inputType="textEmailAddress"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText7" />

<EditText
android:id="@+id/editText6"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_ph"
android:inputType="phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText5" />

<EditText
android:id="@+id/editText7"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="@string/edit_age"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>

activity_display_message.xml:

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="@android:color/background_dark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</android.support.constraint.ConstraintLayout>

如有任何帮助,我们将不胜感激。我是 Android 编程的新手,如果我犯了愚蠢的错误,请原谅,请随时指出。谢谢:)

最佳答案

您在第二个 Activity 中传递了错误的键,这就是为什么您的 Activity 在 Oncreate() 中崩溃的原因。

public static final String EXTRA_MESSAGE_NAME="com.example.myfirstapp.MESSAGE";
public static final String EXTRA_MESSAGE_AGE="com.example.myfirstapp.MESSAGE";
public static final String EXTRA_MESSAGE_EMAIL="com.example.myfirstapp.MESSAGE";
public static final String EXTRA_MESSAGE_PH="com.example.myfirstapp.MESSAGE";

上面你已经为 bundle 声明了键,但是在第二个 Activity 中你犯了错误。

  Intent intent=getIntent();
Bundle extras=intent.getExtras();
String message=extras.getString("EXTRA_MESSAGE_NAME");
String message1=extras.getString("EXTRA_MESSAGE_AGE");
String message2=extras.getString("EXTRA_MESSAGE_EMAIL");
String message3=extras.getString("EXTRA_MESSAGE_PH");

你需要像下面这样传递 key :

 String message=extras.getString(MainActivity.EXTRA_MESSAGE_NAME);

关于java - Android,接受用户的值并在下一个 Activity 中显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43292891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com