gpt4 book ai didi

java - 将包传递给 if 语句

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:38 24 4
gpt4 key购买 nike

Activity Claims---->有2个按钮和一个textView(获取总金额)

Activity Project1 和 Petrol-----> 一个 editText 和一个保存按钮

Activity 声明

 button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogRadio(a1);

}
});

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogRadio(a2);

}
});

public void AlertDialogRadio(final int k) {
final CharSequence[] ClaimsModel = {"Project1", "Petrol"};

AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Claims");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {

public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
intent.putExtra("k",k);
startActivityForResult(intent, 0);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
intent.putExtra("k", k);
startActivityForResult(intent, 1);
}

** Project1 或 Petrol,取决于...)**

 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
text= (EditText)findViewById(R.id.editText1);
Button save=(Button)findViewById(R.id.button12);

save.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent returnIntent = new Intent();
l="A";
text = text.getText().toString();
returnIntent.putExtra("text", text);
returnIntent.putExtra("l", l);
final int k1 = getIntent().getExtras().getInt("k");
returnIntent.putExtra("k1", k1);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});

}

@Override
public void onBackPressed()
{
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}

是否可以将bundle传递给if语句?我想将一个值传递给另一个案例,最后将两个值相加。我怎样才能实现这个目标?现在我只得到了值 asbs,但没有得到值 amount。我确实相信它需要向两个 if 语句 添加一个 bundle...

Continue Claims.java...

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
int button = data.getIntExtra("k1", 0); // to check which button was pressed
long a=0;
long as=0;
long bs=0;
String result;
String result1;

if (button == 1) {
switch (requestCode) {
case 0:
result = data.getStringExtra("text");
String b = data.getStringExtra("l");
as=Long.parseLong(result);
c.setText(" " + b + "------" + "RM " + result);
Toast.makeText(getActivity(),as+"", Toast.LENGTH_LONG).show();
break;

case 1:
result = data.getStringExtra("text");
String b1 = data.getStringExtra("l");
as=Long.parseLong(result);
c.setText(" " + b1 + "------" + "RM " + result);
Toast.makeText(getActivity(),as+"", Toast.LENGTH_LONG).show();
break;


}
if(button==2)
{
switch (requestCode) {
case 0:
result1 = data.getStringExtra("text");
String b = data.getStringExtra("l");
bs=Long.parseLong(result1);
d.setText(" " + b + "------" + "RM " + result1);
Toast.makeText(getActivity(),bs+"", Toast.LENGTH_LONG).show();
break;

case 1:
result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("l");
bs=Long.parseLong(result1);
d.setText(" " + b1 + "------" + "RM " + result1);
Toast.makeText(getActivity(),bs+"", Toast.LENGTH_LONG).show();
break;

}

}

else if(requestCode==CAMERA_REQUEST_CODE)
{

}

long amount=as+bs;
Toast.makeText(getActivity(),amount+"", Toast.LENGTH_LONG).show();

我指的是How can i pass a string value, that has been created in an if statement, through a bundle inside another if statement?但答案对我来说很不清楚。

claim 图片

![enter image description here

toast 应该显示 1 12 13,但我得到 1 12 12。 似乎无法添加值 1!

最佳答案

您应该将在 onActivityResult 中传递的值存储在 Activity 的变量中,然后直接读取 Toast 的这些变量。

public class Claims{
long a, as, bs;

public void onActivityResult(int requestCode, int resultCode, Intent data) {
int button = data.getIntExtra("k1", 0);
String result;

if (button == 1) {
result = data.getStringExtra("text");
as=Long.parseLong(result);
} else if(button==2) {
result = data.getStringExtra("text");
bs=Long.parseLong(result1);
}

long amount=as+bs;
Toast.makeText(getActivity(),amount+"", Toast.LENGTH_LONG).show();
}
}

不过,您仍然需要根据您的标准初始化您的值。

关于java - 将包传递给 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33405648/

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