我是android studio的新手,我正在使用相同的2.3版本。我正在学习intent的概念。我创建了2个活动:Apples和Bananas.Each有一个textView和一个Button.Apples有一个editText。用户在editText中输入并单击按钮。然后香蕉打开,香蕉的textView变为。
我的问题是:单击按钮后,Bananas中的textView正在消失。我发现这是由于香蕉活动的java代码中的代码行所致:
//code for Apples activity
package com.awani.intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.EditText;
public class Apples extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apples);
}
public void onClick(View view) {
//create instance of intent class
//this tells the app that the following is the activity which we want to launch on click of the button
Intent I= new Intent(this,Bananas.class);
//Refer to the input textfield in the activity
final EditText applesInput=(EditText)findViewById(R.id.applesInput);
//get the inuput from user
String userMessage=applesInput.getText().toString();
//pass this info to the net activity which will appear afterr this click
I.putExtra("applesmessage",userMessage);//this method takes info from current activity in the form of k ey-value pair
//launch the activity
startActivity(I);
}
}
//code for activity Bananas
package com.awani.intent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
public class Bananas extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bananas);
//we need this class to accept the extra infromation passed to it by some other activity
Bundle applesData=getIntent().getExtras();//this extra information is stored in applesData
//test if the data is null or there is something(so that to avoid error)
if(applesData==null){
return;
}
//now as error is taken care of,move forward
String applesMessage=applesData.getString("applesMessage");//in the variable applesMessage we are storing the string we got from user by passing the key which we passed in putExtra method
//refer to the textview in the banana activity
final TextView bananasText=(TextView)findViewById(R.id.bananasText);
bananasText.setText(applesMessage);//we change the text Bananas to the text which was input by user
}
public void onClick(View view) {
//create instance of intent class
//this tells the app that the following is the activity which we want to launch on click of the button
Intent I= new Intent(this,Apples.class);
//launch the activity
startActivity(I);
}
}
但我不知道应该做些什么改变。 以下是这两项活动的代码。
group by
答案 0 :(得分:0)
在Bananas活动中更改获取值的关键,因为在First活动中,您将发送密钥为" applesmessage"在香蕉中你收到的是#34; applesMessage"
String applesMessage=applesData.getString("applesMessage");
到
String applesMessage=applesData.getString("applesmessage");