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

标签 java android xml

我应该询问用户姓名、年龄、电子邮件和电话号码,并在另一个 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/

相关文章:

PYTHON 解析位于目录中的多个文件 XML 并上传 CSV 文件中的数据

java - Twitter Digits 检查是否已经进行了身份验证

java - 以 JSON 格式将行值发送到 Web 服务 (Android)

java - ContentValues Insert 始终返回 -1

java - AsyncTask 在第一次尝试中不会在 onPostExecute 上返回 JSON 响应,但从第二次尝试开始工作正常

android - 在 Microsoft AppCenter 中构建多个构建变体

android - 使用带有导航架构组件的底部菜单栏时如何从工具栏中删除后退按钮

android - 方法 setTitle 的工具栏错误

javascript - 如何使用 XSLT 匹配元素并输出 HTML?

java - 使用 XSD 将 XML/RDF 转换为 Java 对象