我刚刚接触到android studio,我遇到了这个问题。 我有3个不同的标签与不同的片段,我有复选框。我使用comomunicator为浮动片段设置值。但每次我更改选项卡并选择其他选项卡中的复选框。第一个选项卡的值将不再计数,它将开始为零,因此我计划在该片段中创建一个全局变量而不是局部变量,因为在java中我使用全局变量来提高效率。所以我的问题是如何创建一个全局变量并将所有值存储到它。
继承人我的Tab1:
public class Tab1 extends android.support.v7.app.AppCompatDialogFragment implements View.OnClickListener {
CheckBox mc1,mc2,mc3,mc4,mc5,mc6,mc7,mc8,mc9,mc10,mc11,mc12;
communicator comm;
int total;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
comm=(communicator)getActivity();
mc1=(CheckBox)getActivity().findViewById(R.id.mc1);
mc1.setOnClickListener(this);
mc2=(CheckBox)getActivity().findViewById(R.id.mc2);
mc2.setOnClickListener(this);
mc3=(CheckBox)getActivity().findViewById(R.id.mc3);
mc3.setOnClickListener(this);
mc4=(CheckBox)getActivity().findViewById(R.id.mc4);
mc4.setOnClickListener(this);
mc5=(CheckBox)getActivity().findViewById(R.id.mc5);
mc5.setOnClickListener(this);
mc6=(CheckBox)getActivity().findViewById(R.id.mc6);
mc6.setOnClickListener(this);
mc7=(CheckBox)getActivity().findViewById(R.id.mc7);
mc7.setOnClickListener(this);
mc8=(CheckBox)getActivity().findViewById(R.id.mc8);
mc8.setOnClickListener(this);
mc9=(CheckBox)getActivity().findViewById(R.id.mc9);
mc9.setOnClickListener(this);
mc10=(CheckBox)getActivity().findViewById(R.id.mc10);
mc10.setOnClickListener(this);
mc11=(CheckBox)getActivity().findViewById(R.id.mc11);
mc11.setOnClickListener(this);
mc12=(CheckBox)getActivity().findViewById(R.id.mc12);
mc12.setOnClickListener(this);
}
@Override
public void onClick(View v) {
boolean checked = ((CheckBox) v).isChecked();
switch (v.getId()){
case R.id.mc1:
if(checked){
total=total+400;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-400;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc2:
if(checked){
total=total+250;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-250;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc3:
if(checked){
total=total+350;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-350;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc4:
if(checked){
total=total+400;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-400;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc5:
if(checked){
total=total+250;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-250;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc6:
if(checked){
total=total+250;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-250;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc7:
if(checked){
total=total+250;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-250;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc8:
if(checked){
total=total+200;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-200;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc9:
if(checked){
total=total+150;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-150;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc10:
if(checked){
total=total+150;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-150;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc11:
if(checked){
total=total+450;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-450;
comm.respond("Total:\n"+total+" Php.");
}
break;
case R.id.mc12:
if(checked){
total=total+550;
comm.respond("Total:\n"+total+" Php.");
}
else{
total=total-550;
comm.respond("Total:\n"+total+" Php.");
}
break;
}
}
}
继承人我的Tab2:
public class Tab2 extends android.support.v7.app.AppCompatDialogFragment implements View.OnClickListener{
CheckBox aoc1,aoc2,aoc3,aoc4;
communicator comm;
int total;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab2,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
comm=(communicator)getActivity();
aoc1=(CheckBox)getActivity().findViewById(R.id.aoc1);
aoc1.setOnClickListener(this);
aoc2=(CheckBox)getActivity().findViewById(R.id.aoc2);
aoc2.setOnClickListener(this);
aoc3=(CheckBox)getActivity().findViewById(R.id.aoc3);
aoc3.setOnClickListener(this);
aoc4=(CheckBox)getActivity().findViewById(R.id.aoc4);
aoc4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
boolean checked = ((CheckBox) v).isChecked();
switch (v.getId()) {
case R.id.aoc1:
if (checked) {
total = total + 50;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 50;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.aoc2:
if (checked) {
total = total + 100;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 100;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.aoc3:
if (checked) {
total = total + 100;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 100;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.aoc4:
if (checked) {
total = total + 150;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 150;
comm.respond("Total:\n" + total + " Php.");
}
break;
}
}
}
继承人我的Tab3:
public class Tab3 extends android.support.v7.app.AppCompatDialogFragment implements View.OnClickListener{
CheckBox fc1,fc2,fc3,fc4,fc5,fc6,fc7,fc8;
int total=0;
communicator comm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab3,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
comm=(communicator)getActivity();
fc1=(CheckBox)getActivity().findViewById(R.id.fc1);
fc1.setOnClickListener(this);
fc2=(CheckBox)getActivity().findViewById(R.id.fc2);
fc2.setOnClickListener(this);
fc3=(CheckBox)getActivity().findViewById(R.id.fc3);
fc3.setOnClickListener(this);
fc4=(CheckBox)getActivity().findViewById(R.id.fc4);
fc4.setOnClickListener(this);
fc5=(CheckBox)getActivity().findViewById(R.id.fc5);
fc5.setOnClickListener(this);
fc6=(CheckBox)getActivity().findViewById(R.id.fc6);
fc6.setOnClickListener(this);
fc7=(CheckBox)getActivity().findViewById(R.id.fc7);
fc7.setOnClickListener(this);
fc8=(CheckBox)getActivity().findViewById(R.id.fc8);
fc8.setOnClickListener(this);
}
@Override
public void onClick(View v) {
boolean checked = ((CheckBox) v).isChecked();
switch (v.getId()) {
case R.id.fc1:
if (checked) {
total = total + 70;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 70;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.fc2:
if (checked) {
total = total + 80;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 80;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.fc3:
if (checked) {
total = total + 300;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 300;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.fc4:
if (checked) {
total = total + 300;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 300;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.fc5:
if (checked) {
total = total + 200;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 200;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.fc6:
if (checked) {
total = total + 260;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 260;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.fc7:
if (checked) {
total = total + 180;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 180;
comm.respond("Total:\n" + total + " Php.");
}
break;
case R.id.fc8:
if (checked) {
total = total + 180;
comm.respond("Total:\n" + total + " Php.");
} else {
total = total - 180;
comm.respond("Total:\n" + total + " Php.");
}
break;
}
}
}
提前致谢! PS。我使用communicator在浮动片段中抛出值。
答案 0 :(得分:1)
使用应用程序类。创建一个扩展Application的类,在类中声明所有变量并为变量创建getter和setter。您现在可以使用该类来存储和检索任何片段中的数据。
This链接可以帮助您入门。