我对android很新,并且通过制作表单来尝试我的手。除了我输入表单的数据无法正确显示之外,我得到了大部分内容。这是代码,任何帮助将不胜感激。
MYACTIVITY.JAVA:
package com.example.win81.myfirstapp.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.win81.myfirstapp.DisplayMessageActivity;
import com.example.win81.myfirstapp.R;
import com.example.win81.myfirstapp.interfaces.IConstants;
public class MyActivity extends AppCompatActivity implements IConstants {
public final static String EXTRA_MESSAGE = "com.example.win81.myfirstapp.MESSAGE";
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
EditText editText5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
editText5 = (EditText) findViewById(R.id.editText5);
Intent intent = getIntent();
String name = intent.getStringExtra("key");
Button reset = (Button) findViewById(R.id.reset);
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
runOnUiThread(new Runnable() {
@Override
public void run() {
editText1.setText("");
editText2.setText("");
editText3.setText("");
editText4.setText("");
editText5.setText("");
}
});
}
});
}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
String message = editText1.getText().toString();
intent.putExtra(KEY_NAME, message);
String message1 = editText2.getText().toString();
intent.putExtra(KEY_EMAIL, message1);
String message2 = editText3.getText().toString();
intent.putExtra(KEY_PHONE, message2);
String message3 = editText4.getText().toString();
intent.putExtra(KEY_ADDRESS, message3);
String message4 = editText5.getText().toString();
intent.putExtra(KEY_PASS, message4);
startActivity(intent);
}
}
ACTIVITY_MY.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"
android:background="#006699"><![CDATA[
tools:context="com.example.win81.myfirstapp.activity.MyActivity">
]]>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="NAME : "
android:id="@+id/textView1"
android:layout_alignBottom="@+id/editText"
android:layout_alignRight="@+id/textView3"
android:layout_alignEnd="@+id/textView3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText1"
android:textColor="#ffffff"
android:maxLength="30"
android:maxHeight="15dp"
android:singleLine="true"
android:layout_alignLeft="@+id/editText3"
android:layout_alignStart="@+id/editText3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Email : "
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="61dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText2"
android:maxLength="50"
android:textColor="#ffffff"
android:layout_alignTop="@+id/textView2"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone : "
android:id="@+id/textView3"
android:layout_marginTop="55dp"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText3"
android:maxLength="10"
android:phoneNumber="true"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_alignTop="@+id/textView3"
android:layout_toRightOf="@+id/textView4"
android:layout_toEndOf="@+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Address : "
android:id="@+id/textView4"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editText4"
android:maxLength="200"
android:maxLines="3"
android:textColor="#ffffff"
android:layout_alignTop="@+id/textView4"
android:layout_toRightOf="@+id/textView5"
android:layout_toEndOf="@+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Password : "
android:id="@+id/textView5"
android:layout_marginTop="60dp"
android:layout_below="@+id/editText4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText5"
android:layout_alignTop="@+id/textView5"
android:layout_alignLeft="@+id/editText4"
android:layout_alignStart="@+id/editText4"
android:maxLength="20"
android:maxLines="1"
android:password="true"
android:singleLine="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:id="@+id/button1"
android:layout_marginTop="49dp"
android:textColor="#ffffff"
android:onClick="sendMessage"
android:layout_below="@+id/editText5"
android:layout_toRightOf="@+id/textView2"
android:layout_toEndOf="@+id/textView2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLEAR ALL"
android:id="@+id/reset"
android:textColor="#a43030"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/textClock"
android:layout_toEndOf="@+id/textClock" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textClock"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
DISPLAYMESSAGEACTIVITY.JAVA
package com.example.win81.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.win81.myfirstapp.activity.MyActivity;
import com.example.win81.myfirstapp.interfaces.IConstants;
public class DisplayMessageActivity extends AppCompatActivity implements IConstants{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.KEY_NAME);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
layout.addView(textView);
Intent intent1 = getIntent();
String message1 = intent1.getStringExtra(MyActivity.KEY_EMAIL);
TextView textView1 = new TextView(this);
textView.setTextSize(20);
textView1.setText(message1);
layout.addView(textView1);
Intent intent2 = getIntent();
String message2 = intent2.getStringExtra(KEY_PHONE);
TextView textView2 = new TextView(this);
textView.setTextSize(10);
textView2.setText(message2);
layout.addView(textView2);
Intent intent3 = getIntent();
String message3 = intent3.getStringExtra(KEY_ADDRESS);
TextView textView3 = new TextView(this);
textView.setTextSize(40);
textView3.setText(message3);
layout.addView(textView3);
Intent intent4 = getIntent();
String message4 = intent4.getStringExtra(KEY_PASS);
TextView textView4 = new TextView(this);
textView.setTextSize(40);
textView4.setText(message4);
layout.addView(textView4);
}
}
ACTIVITY_DISPLAY_MESSAGE.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:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#006699"
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.win81.myfirstapp.DisplayMessageActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linearLayout">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NAME : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:maxHeight="15dp"
android:maxLength="30"
android:singleLine="true"
android:textColor="#ffffff" />
</LinearLayout>
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:layout_marginTop="61dp"
android:text="Email : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:maxLength="50"
android:textColor="#ffffff"
android:layout_alignBottom="@+id/email"
android:layout_toRightOf="@+id/Address"
android:layout_toEndOf="@+id/Address" />
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone : "
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="42dp"
android:layout_below="@+id/email"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:maxLength="10"
android:phoneNumber="true"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_above="@+id/Address"
android:layout_alignLeft="@+id/textClock"
android:layout_alignStart="@+id/textClock" />
<TextView
android:id="@+id/Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address : "
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_below="@+id/phone"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="49dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:maxLength="200"
android:maxLines="3"
android:textColor="#ffffff"
android:layout_above="@+id/password"
android:layout_alignRight="@+id/reset"
android:layout_alignEnd="@+id/reset" />
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password : "
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="44dp"
android:layout_below="@+id/Address"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="SUBMIT"
android:textColor="#ffffff"
android:layout_above="@+id/textClock"
android:layout_alignRight="@+id/password"
android:layout_alignEnd="@+id/password"
android:layout_marginBottom="31dp" />
<Button
android:id="@+id/reset"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLEAR ALL"
android:textColor="#a43030"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/textClock"
android:layout_toEndOf="@+id/textClock" />
<TextClock
android:id="@+id/textClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:maxLength="20"
android:maxLines="1"
android:password="true"
android:singleLine="true"
android:layout_toEndOf="@+id/textView3"
android:layout_alignBottom="@+id/password"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
答案 0 :(得分:0)
这是因为您在DisplayMessageActivity中创建了新的TextView。相反,您应该使用在activity_display_message.xml布局中添加的TextView。
使用findViewById来获取您在第1次活动中所做的视图参考。