簡體   English   中英

Android,接受用戶的值並在下一個活動中顯示它們

[英]Android, to accept values from the user and to show them in next activity

我應該向用戶詢問姓名,年齡,電子郵件和電話號碼,並在另一個活動中顯示這些值。 Android開發培訓(來自Google)中顯示的示例對一個變量(鍵/值對)進行了相同的處理。 但是,在這里我必須傳遞4個值。 在查找如何執行此操作時,我遇到了Bundle。 使用相同的內容,第二個活動不會顯示任何內容,即,盡管有四個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);
}
}

第二個活動,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編程非常陌生,因此,如果我犯了愚蠢的錯誤,請原諒我,並隨時指出。 謝謝 :)

您在第二個活動中傳遞了錯誤鍵,這就是為什么您的活動可能在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");

您需要像下面的行那樣傳遞密鑰:

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

您的問題是,在MainActivity聲明了四個具有相同值的 變量 當您將用戶輸入放到Bundle中時,它將始終覆蓋您之前插入的值,因為只能有一個單獨的key 因此,您應該編輯變量:

public static final String EXTRA_MESSAGE_NAME="com.example.myfirstapp.MESSAGE_NAME";
public static final String EXTRA_MESSAGE_AGE="com.example.myfirstapp.MESSAGE_AGE";
public static final String EXTRA_MESSAGE_EMAIL="com.example.myfirstapp.MESSAGE_EMAIL";
public static final String EXTRA_MESSAGE_PH="com.example.myfirstapp.MESSAGE_PH";


下一個問題在於DisplayMessageActivity類。 您正在試圖找回在你插入的數據MainActivity就在你聲明的變量的名稱 MainActivity 因此, getString()方法返回null ,因為它們不是變量名的鍵。 您需要使用變量檢索該數據。 根據此,將代碼更改為此:

String message=extras.getString(MainActivity.EXTRA_MESSAGE_NAME);
String message1=extras.getString(MainActivity.EXTRA_MESSAGE_AGE);
String message2=extras.getString(MainActivity.EXTRA_MESSAGE_EMAIL);
String message3=extras.getString(MainActivity.EXTRA_MESSAGE_PH);

由於MainActivity中的變量是public static ,因此您可以訪問它們而無需MainActivity類的實例。

因此請記住,您可以使用變量而不是name來訪問保存的數據。


我的提示: 閱讀一些Java和Android教程,尤其是Java教程 ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM