我有这个函数,它将Group.java中的RadioButton值调用到Add.java。我在名为Status.java的另一个活动上使用相同的函数。现在,每次从Group.java或Status.java中单击时,结果都会重复。每次我点击RadioButton,我的EditText都会消失。
Group.java
RadioGroup radiog1;
RadioButton radio1, radio2, radio3, radio4, radio5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);
radiog1 = (RadioGroup) findViewById(R.id.radiog1);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio4 = (RadioButton) findViewById(R.id.radio4);
radio5 = (RadioButton) findViewById(R.id.radio5);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .8), (int) (height * .6));
radio1.setOnClickListener(this);
radio2.setOnClickListener(this);
radio3.setOnClickListener(this);
radio4.setOnClickListener(this);
radio5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent returnIntent = getIntent();
switch (v.getId()) {
case (R.id.radio1):
returnIntent.putExtra("GroupTag", "" + radio1.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio2):
returnIntent.putExtra("GroupTag","" + radio2.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio3):
returnIntent.putExtra("GroupTag", "" + radio3.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio4):
returnIntent.putExtra("GroupTag", "" + radio4.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio5):
returnIntent.putExtra("GroupTag","" + radio5.getText());
setResult(RESULT_OK,returnIntent);
finish();
break;
}
}}
Status.java
RadioButton rb1, rb2, rb3, rb4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .8), (int) (height * .6));
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb3 = (RadioButton) findViewById(R.id.rb3);
rb4 = (RadioButton) findViewById(R.id.rb4);
rb1.setOnClickListener(this);
rb2.setOnClickListener(this);
rb3.setOnClickListener(this);
rb4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent returnIntent = getIntent();
switch (v.getId()) {
case (R.id.rb1):
returnIntent.putExtra("StatusTag", "" + rb1.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.rb2):
returnIntent.putExtra("StatusTag","" + rb2.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.rb3):
returnIntent.putExtra("StatusTag", "" + rb3.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.rb4):
returnIntent.putExtra("StatusTag","" + rb4.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
}
}}
Add.java
ImageButton ibtn, ibtn2, ibtn3, ibtn4,ibtn5;
TextView tvgroup;
TextView tvstatus;
int groupRequestCode;
int statusRequestCode;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
tvgroup = (TextView) findViewById(R.id.tvgroup);
tvstatus = (TextView) findViewById(R.id.tvstatus);
ibtn = (ImageButton) findViewById(R.id.ibtn);
ibtn2 = (ImageButton) findViewById(R.id.ibtn2);
ibtn3 = (ImageButton) findViewById(R.id.ibtn3);
ibtn4 = (ImageButton) findViewById(R.id.ibtn4);
ibtn5 = (ImageButton) findViewById(R.id.ibtn5);
ibtn.setOnClickListener(this);
ibtn2.setOnClickListener(this);
ibtn3.setOnClickListener(this);
ibtn4.setOnClickListener(this);
ibtn5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case (R.id.ibtn):
startActivityForResult(new Intent(this, Group.class), groupRequestCode);
break;
case (R.id.ibtn2):
startActivity(new Intent(this, Due_Date.class));
break;
case (R.id.ibtn3):
startActivity(new Intent(this,DueTime.class));
break;
case (R.id.ibtn4):
startActivityForResult(new Intent(this, Status.class), statusRequestCode);
break;
case (R.id.ibtn5):
startActivity(new Intent(this,Assignees.class));
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == groupRequestCode) { // here you come back from Group.java
if(resultCode == RESULT_OK){
// do your stuff here
String textViewName = data.getStringExtra("GroupTag");
tvgroup.setText(textViewName);
}
}
if (requestCode == statusRequestCode) { // here you come back from Status.java
if(resultCode == RESULT_OK){
// do your stuff here
String status = data.getStringExtra("StatusTag");
tvstatus.setText(status);
}
}
}}
结果:
真的很感激任何形式的帮助。
答案 0 :(得分:1)
我建议您使用startActivityForResult()方法在活动之间传递数据。
所以你需要改变你的代码:
1)删除此代码:
Bundle extra = getIntent().getExtras();
if (extra != null) {
String textViewName = extra.getString("SomeTag");
tvgroup.setText(textViewName);
}
Bundle extra2 = getIntent().getExtras();
if (extra2 != null) {
String status = extra2.getString("SomeTag");
tvstatus.setText(status);
}
2)
startActivity(new Intent(this,Group.class));
到startActivityForResult(new Intent(this,Group.class), groupRequestCode);
startActivity(new Intent(this,Status.class));
到startActivityForResult(new Intent(this,Status.class), statusRequestCode);
PS:groupRequestCode
应与statusRequestCode
不同(例如1和2)。
3)Overrid onAcitivtyResult()方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == groupRequestCode) { // here you come back from Group.java
if(resultCode == RESULT_OK){
// do your stuff here
String textViewName = data.getStringExtra("GroupTag");
tvgroup.setText(textViewName);
}
}
if (requestCode == statusRequestCode) { // here you come back from Status.java
if(resultCode == RESULT_OK){
// do your stuff here
String status = data.getStringExtra("StatusTag");
tvstatus.setText(status);
}
}
}
PS:如果你有很多请求代码,你可以使用switch
而不是if
集团。
更改
Intent intent = new Intent(Group.this, Add.class);
intent.putExtra("SomeTag", "" + radio1.getText());
startActivity(intent);
到
Intent returnIntent = getIntent();
returnIntent.putExtra("GroupTag","" + radio1.getText());
setResult(RESULT_OK,returnIntent);
finish();
=>对RadioButtons的其余部分做同样的事情
更改
Intent intent2 = new Intent(Status.this, Add.class);
intent2.putExtra("SomeTag", "" + rb2.getText());
startActivity(intent2);
到
Intent returnIntent = getIntent();
returnIntent.putExtra("StatusTag","" + rb2.getText());
setResult(RESULT_OK, returnIntent);
finish();
=>对RadioButtons的其余部分做同样的事情