所以我正在创建一个简单的应用程序,只需要从用户那里获取一些信息,即名称,地址等,并将其放在下一个活动的textview中,但是当我输入值并转到下一个活动时,没有什么显示在TextViews中。
这是我的第一个活动
public class MainActivity extends Activity {
public String Name;
public String Age;
public String Address;
public String City;
public String phoneno;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText name = (EditText) findViewById(R.id.editText1);
Name = name.getText().toString();
EditText Amge = (EditText) findViewById(R.id.agee);
Age = Amge.getText().toString();
EditText Address2 = (EditText) findViewById(R.id.address);
Address = Address2.getText().toString();
EditText City2 = (EditText) findViewById(R.id.city);
City = City2.getText().toString();
EditText phone2 = (EditText) findViewById(R.id.phone);
phoneno = phone2.getText().toString();
final ImageView D = (ImageView) findViewById(R.id.done);
D.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, second.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);}}
这是我的第二个活动
public class second extends MainActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView N1 = (TextView) findViewById(R.id.name1);
N1.setText(Name);
TextView A1 = (TextView) findViewById(R.id.age1);
A1.setText(Age);
TextView Ad1 = (TextView) findViewById(R.id.address1);
Ad1.setText(Address);
TextView C1 = (TextView) findViewById(R.id.city1);
C1.setText(City);
TextView P1 = (TextView) findViewById(R.id.phone1);
P1.setText(phoneno);}
public static void main(String[] args) {
// TODO Auto-generated method stub
}}
这是我的主要活动的xml
<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:orientation="horizontal"
android:background="@drawable/bg"
tools:context="com.example.randomtests.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:textSize="21dp"
android:textColor="#ffffff"
android:text="Please enter the required info"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView1"
android:src="@drawable/bar" />
<EditText
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/agee"
android:layout_below="@+id/agee"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="@string/address"
android:inputType="textPostalAddress"
android:textColor="#000000" />
<EditText
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/address"
android:layout_below="@+id/address"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="@string/city"
android:textColor="#000000" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:gravity="top"
android:textColor="#000000"
android:hint="@string/namu"
android:inputType="textPersonName" />
<EditText
android:id="@+id/agee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="@string/age"
android:inputType="phone"
android:textColor="#000000" />
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/city"
android:layout_below="@+id/city"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="@string/phone"
android:inputType="phone"
android:textColor="#000000" />
<ImageView
android:id="@+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/phone"
android:layout_marginRight="14dp"
android:layout_marginTop="12dp"
android:src="@drawable/button" />
这是我的第二个活动的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg" >
<TextView
android:id="@+id/info1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#ffffff"
android:textSize="33dp"
android:layout_centerHorizontal="true"
android:text="@string/yourinfo"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/info1"
android:textColor="#ffffff"
android:layout_marginLeft="19dp"
android:layout_marginTop="26dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/age1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/name1"
android:layout_below="@+id/name1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/address1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/age1"
android:layout_below="@+id/age1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/city1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/address1"
android:layout_below="@+id/address1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/phone1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/city1"
android:layout_below="@+id/city1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
答案 0 :(得分:1)
当您在此处声明Intent
时:
Intent intent = new Intent(MainActivity.this, second.class);
尝试使用Extras
根据点击的视图
例如:
intent.putExtra(Intent.EXTRA_INTENT, **Your String Here**);
在接收意图的活动中,使用以下内容:
String data = intent.getStringExtra(Intent.EXTRA_INTENT);
从意图中获取数据
答案 1 :(得分:1)
宣布您的意图后,请尝试使用此
发送数据//this use key-value
intent.putExtra("USER_NAME" , "your string");
intent.putExtra("USER_AGE" , "your string");
等等。
和其他活动尝试使用
Bundle extra = getIntent().getExtras();
String userName = extra.getString("USER_NAME");
String userAge = extra.getString("USER_AGE");