我是android studio的初学者,我想编写一个包含3个活动的代码。 第一个用于启动app.2用于显示英语单词,第三个用于显示英语单词及其在两个文本中的描述。
我想传输英文单词本身的数据及其描述和单词的数量,逐一显示单词。
我在教程剪辑的帮助下编写了代码,但它没有工作,第三个活动没有显示任何内容。
这些是我的代码:
主要活动:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view)
{
Intent i = new Intent(this,ActivityOne.class);
startActivity(i);
}
}
ActivityOne
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ActivityOne extends AppCompatActivity {
int a=0;
String E , P;
private Button show;
private TextView Eword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Eword = (TextView) findViewById(R.id.Eword);
show = (Button) findViewById(R.id.show);
}
public void show(View view)
{
Intent intent = new Intent(this,ActivityTwo.class);
startActivity(intent);
int a = getIntent().getIntExtra("counter",0);
Eword.setText(Eng[a]);
E = Eword.getText().toString();
P = Fa[a];
a++;
intent.putExtra("counter",a);
intent.putExtra("EWord",E);
intent.putExtra("PWord",P);
}
private String[] Eng = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
private String[] Fa = {
"Abundance Description",
"Anxiety Description",
"Bruxism Description",
"Discipline Description",
"Drug Addiction Description"
};
}
ActivityTwo
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
TextView Eng , Fa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Eng = (TextView) findViewById(R.id.eng);
Fa = (TextView) findViewById(R.id.fa);
// recieve data from activity one
String EWord = getIntent().getStringExtra("EWord");
String PWord = getIntent().getStringExtra("PWord");
int a = getIntent().getIntExtra("counter",0);
Eng.setText(EWord);
Fa.setText(PWord);
}
public void iknow(View view)
{
Intent myIntent = new Intent(this,ActivityOne.class);
startActivity(myIntent);
int a = getIntent().getIntExtra("counter",0);
myIntent.putExtra("counter",a);
}
public void idknow(View view)
{
Intent myIntentTwo = new Intent(this,ActivityOne.class);
startActivity(myIntentTwo);
int a = getIntent().getIntExtra("counter",0);
myIntentTwo.putExtra("counter",a);
}
}
它显示了这个结果:
有人可以帮助我?????
答案 0 :(得分:0)
(1)为了让您入门,您需要在开始活动之前将变量放入意图中。 (2)。我不认为让活动开始两个是个好主意,然后可以开始一个。最好只关闭第二个活动然后自动返回一个活动。 (3)要将数据传回您正在返回的活动,您需要startActivityForResult https://developer.android.com/training/basics/intents/result.html
活动一使用startActivityForResult启动活动二。活动二返回一个意图(这是结果意图),活动一返回onActivityResult。
看看这个:Android: how to make an activity return results to the activity which calls it?