如何在另一个活动的Textview中传递用户EditText输入

时间:2016-09-15 14:33:15

标签: java android xml android-layout android-studio

我的主要活动中有2个文本框,其中包含用户名和ID号。 单击编辑按钮时,用户将被发送到第二个活动,在那里他们可以编辑他们的姓名和身份证号码。 然后,当他们单击“保存”时,编辑的输入应覆盖第一个活动中两个现有文本视图中的书面文本。 但是当我点击“保存”按钮时,我的应用程序崩溃了......

此外,主要活动还有捕获概况图。 feauture,但在横向模式下,我拍摄的图像。消失。

我是Android的新手,这是一个周日到期的学校作业......有人可以帮助我!!

这是我的第一个活动的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.solveigdoan.cameraactivity.SecondActivity2"
android:background="#F0FFFF">


<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="8"
android:id="@+id/EditName"
android:layout_marginTop="42dp"
android:hint="Solveig Mortensen"
android:layout_marginLeft="160dp"
android:background="#87CEEB"
android:textSize="20dp"
android:textColor="#000000" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/EditName"
android:text="@string/edit_name"
android:textSize="20dp"
android:textColor="#000000"
android:id="@+id/NameLabel" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="8"
android:id="@+id/EditID"
android:hint="123456"
android:layout_below="@+id/EditName"
    android:layout_alignLeft="@+id/EditName"
    android:layout_alignStart="@+id/EditName"
    android:layout_marginTop="40dp"
    android:background="#87CEEB"
    android:textSize="20dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/EditID"
    android:text="@string/edit_id"
    android:textSize="20dp"
    android:textColor="#000000"
    android:id="@+id/IdLabel" />

<CheckBox
    android:id="@+id/Yes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Y"
    android:onClick="onCheckboxClicked"
    android:layout_marginTop="27dp"
    android:layout_below="@+id/AndrStatus"
    />


<CheckBox android:id="@+id/NO"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/N"
    android:onClick="onCheckboxClicked"
    android:layout_marginTop="30dp"
    android:layout_below="@+id/Yes"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cancel"
    android:id="@+id/buttonCancel"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textColor="#000000"
    android:background="#87CEEB" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/save_btn"
    android:id="@+id/buttonSave"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textColor="#000000"
    android:background="#87CEEB" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/android_developer"
    android:id="@+id/AndrStatus"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/ID"
    android:layout_alignStart="@+id/ID"
    android:textSize="20dp"
    android:textColor="#000000" />

</RelativeLayout>

AND第二个活动的xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.solveigdoan.cameraactivity.CameraActivity">


<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#F0FFFF" />

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/profile_picture"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="40dp"
    android:textColor="#000000" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:text="@string/take_photo"
    android:layout_below="@+id/textview1"
    android:background="#87CEEB"
    android:layout_marginBottom="5dp"
    android:textColor="#000000" />

<ImageView

    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:scaleType="centerCrop"
    android:layout_below="@+id/button1"
    android:background="#BCC6CC" />



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:ems="9"
    android:id="@+id/Name"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_alignLeft="@+id/ID"
    android:layout_alignStart="@+id/ID"
    android:layout_below="@+id/imageView1"
    android:textSize="20dp"
    android:background="#87CEEB"
    android:textColor="#000000"
    android:hint="Solveig Mortensen" />



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_alignBaseline="@id/Name"
    android:text="@string/name"
    android:textSize="20dp"
    android:textColor="#000000"
    android:id="@+id/NameTitle" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="9"
    android:id="@+id/ID"
    android:layout_alignTop="@+id/Name"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginTop="40dp"
    android:textSize="20dp"
    android:background="#87CEEB"
    android:textColor="#000000"
    android:hint="123456" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_marginTop="30dp"
    android:layout_alignBaseline="@id/ID"
    android:text="@string/id_nr"
    android:textSize="20dp"
    android:textColor="#000000" />

<Button
    android:id ="@+id/push_button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:text="@string/edit_btn"
    android:background="@drawable/button_bg_round"
    android:padding="10dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textSize="12dp"
    android:textColor="#000000" />

<TextView
    android:id="@+id/tvView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:layout_below="@+id/ID"
    android:layout_marginTop="20dp" />
</RelativeLayout>

我的主要java(名为CameraActivity):

public class CameraActivity extends Activity {


Button BtnTakePhoto;
ImageView imgTakenPhoto;
Button push_button;
TextView Name;
TextView ID;

private static final int CAMERA_REQUEST = 1888;
private static final int EDIT_REQUEST = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);


    push_button = (Button) findViewById(R.id.push_button);
    push_button.setOnClickListener(new push_buttonClicker());

    imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);
    BtnTakePhoto = (Button) findViewById(R.id.button1);

    BtnTakePhoto.setOnClickListener(new BtnTakePhotoClicker());


}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data)     {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST) {

        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        imgTakenPhoto.setImageBitmap(thumbnail);
    }

    if (requestCode == EDIT_REQUEST) {

        Bundle b = getIntent().getExtras();
        Name = (TextView) findViewById(R.id.Name);
        ID = (TextView) findViewById(R.id.ID);

        Name.setText(b.getCharSequence("Name"));
        ID.setText(b.getCharSequence("ID nr"));
    }

}

    class BtnTakePhotoClicker implements Button.OnClickListener

{

    @Override
    public void onClick(View v) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }
}class push_buttonClicker implements Button.OnClickListener{
public void onClick(View v){
    Intent Intent = new Intent(CameraActivity.this, SecondActivity2.class);
    CameraActivity.this.startActivityForResult(Intent, EDIT_REQUEST);
}}}

和我的secondActivity.java:

public class SecondActivity2 extends AppCompatActivity {
 TextView tvView;
EditText EditName;
EditText EditID;
Button buttonCancel;
Button buttonSave;
CheckBox checkBox;
TextView Name;
TextView ID;

private static final int RESULT_OK = 10;
//private static final int RESULT_CANCELED = 2;

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


    // CheckBox Yes = (CheckBox) findViewById(R.id.Yes);
   //  CheckBox NO = (CheckBox) findViewById(R.id.NO);

    buttonSave = (Button) this.findViewById(R.id.buttonSave);


    buttonCancel = (Button) findViewById(R.id.buttonCancel);

    EditName = (EditText) findViewById(R.id.EditName);
    EditID = (EditText) findViewById(R.id.EditID);

   buttonSave.setOnClickListener(new OnClickListener() {
                                     public void onClick(View view) {


                                         Intent data = new Intent();


                            data.putExtra("name",    EditName.getText().toString());
                                         data.putExtra("ID", EditID.getText().toString());


                                         setResult(RESULT_OK, data);
                                         finish();
                                     }
                                 });}}

1 个答案:

答案 0 :(得分:0)

EDIT_REQUEST 条件下的 onActivityResult 中更改代码

String name = data.getStringExtra("name");
String ID = data.getStringExtra("ID");

Name.setText(name);
ID.setText(ID);

无需在那里创建一个Bundle,它将解决您的问题。