我成功尝试在三个活动之间传递数据。 那就是:
(数据3)活性1(数据1) - >(数据1)活性2(数据2) - >(DATA2)Activity3)
现在的问题是:
WORD = "word building" DROP = "word built" if WORD FROM EDITTEXT == WORD pass data to Activity1 AND goto Activity1 else if WORD FROM EDITTEXT == DROP pass data to Activity3 AND goto Activity3
以下是名为PickCard.java的Activity1的代码
public class PickCard extends Activity {
String card = "Card Picked";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
displayIntentData();
findViewById(R.id.sendButton).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PickCard.this, BuildWord.class);
intent.putExtra("key", card);
startActivity(intent);
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
以下是Activity2 BuildWord.java的代码
public class BuildWord extends Activity {
String word = "Word Building";
String finished = "Word Built";
EditText simulate = (EditText) findViewById(R.id.dataToSend);
String getdata = simulate.getText().toString();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buildword);
displayIntentData();
if (getdata == word) {
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(BuildWord.this, MainActivity.class);
intent.putExtra("key", getdata);
startActivity(intent);
}
});
} else {
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(BuildWord.this, DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData1);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
以下是名为DropCard.java的Activity3的代码
public class DropCard extends Activity {
String drop = "Card Dropped";
String declared = "User Declared";
boolean won = false;
/*Intent intent = getIntent();
TextView tv = (TextView)findViewById(R.id.intentData2);
Bundle extras=intent.getExtras();*/
String get = "word building";//extras.getString("key");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dropcard);
displayIntentData();
if (get == "word built") {
findViewById(R.id.sendButton2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DropCard.this, Declare.class);
intent.putExtra("key", declared);
startActivity(intent);
}
});
} else {
findViewById(R.id.sendButton2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DropCard.this, MainActivity.class);
intent.putExtra("key", drop);
startActivity(intent);
//notice we dont call finish() here
}
});
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData2);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
答案 0 :(得分:0)
使用
if (getdata.equals(word)){
}
而不是
if (getdata == word){
}
用于比较当前代码中的Strings.as将所有==
替换为equals
答案 1 :(得分:0)
if (getData.getText().toString().equalsIgnoreCase("String name"))//change this line in your code for checking
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,MainActivity.class);
intent.putExtra("key", getdata);
startActivity(intent);
}
});
}
else
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
答案 2 :(得分:0)
在compare
。
default method equalsIgnoreCase()
String name = edit_text.getText().toString();
if(name.equalsIgnoreCase("WellCome"))
{
//CALL YOUR ACTIVITY ONE
}
else
{
//CALL YOUR ACTIVITY THREE
}