gpt4 book ai didi

java - RadioButton 值在不同 Activity 中相互重复

转载 作者:行者123 更新时间:2023-11-30 01:56:14 26 4
gpt4 key购买 nike

我有一个从 Group.java 调用 RadioButton 值到 Add.java 的函数。我在另一个名为 Status.java 的 Activity 中使用相同的函数。现在,每次我从 Group.java 或 Status.java 单击时,结果都会重复。每次单击 RadioButton 时,我的 EditText 都会消失。

组.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;


}
}}

状态.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;



}

}}

添加.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);
}
}
}}

结果:

enter image description here

我们将不胜感激。

最佳答案

我建议你使用startActivityForResult()在 Activity 之间传递数据的方法。

所以你需要这样修改你的代码:

  • Add.java

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) 覆盖 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 block 。

  • Group.java

改变

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 做同样的事情

  • Status.java

改变

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 做同样的事情

关于java - RadioButton 值在不同 Activity 中相互重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32167754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com