使用单选按钮控制变量的最佳方法是什么?

时间:2016-06-26 09:59:23

标签: android radio-button material-design

我对Android编程非常陌生,我无法找到实现目标的可接受方式:

我的主要活动(sortOrder)中有一个变量,我想在不同的活动中使用单选按钮进行设置。目前,我只是通过一个大开关案例从其他活动中更改MainActivity.sortOrder,所以我想知道是否有更优雅的方式。

此外,当我返回MainActivity时,单选按钮“忘记”了他们的选择。是否可以保存选定的单选按钮,并从MainActivity中设置变量?

以下是一些代码:

MainActivity中的

public static String sortOrder;
public static int checkedId;

这些是onCreate中的给定值:

sortOrder = ShipContract.ShipEntry.COLUMN_NAME_NAME + " ASC";
checkedId = R.id.sort_name;

这里是OptionsSort,它是从MainActivity开始的:

public class OptionsSort extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options_sort);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    RadioButton checkedButton = (RadioButton) findViewById(MainActivity.checkedId);
    checkedButton.setChecked(true);
}

public void onRadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    switch(view.getId()) {
        case R.id.sort_name:
            if (checked)
                MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_NAME + " ASC";
            break;
        case R.id.sort_date:
            if (checked)
                MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_DATE + " ASC";
            break;
        case R.id.sort_capacity:
            if (checked)
                MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_CAPACITY + " ASC";
            break;
        case R.id.sort_line:
            if (checked)
                MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_LINE + " ASC";
            break;
        case R.id.sort_displacement:
            if (checked)
                MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_DISPLACEMENT + " ASC";
            break;
        case R.id.sort_speed:
            if (checked)
                MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_SPEED + " ASC";
            break;
        case R.id.sort_power:
            if (checked)
                MainActivity.sortOrder = ShipContract.ShipEntry.COLUMN_NAME_POWER + " ASC";
            break;
    }
    RadioGroup radiogroup = (RadioGroup) findViewById(R.id.sort_group);
    MainActivity.checkedId = radiogroup.getCheckedRadioButtonId();
    finish();
}

}

1 个答案:

答案 0 :(得分:0)

到目前为止,我理解你的问题,你想从活动B中获取sortOder值并将其带到Activity A.如果你使用startActivityForResult并且在活动B中完成所有事情,只需调用setresult方法并给它,这可以实现结果意图并完成此活动B. 在活动A中,您必须覆盖onActivityResult并处理活动B的结果。

像 startActivityForResult(new Intent(this,ActivityB.class),123);

@覆盖

public void onActivityResult(int requestCode,int resultCode,Intentdata){         super.onActivityResult(requestCode,resultCode,data);

    if (resultCode == Activity.RESULT_OK) {

if (requestCode==123)
    {
      String sordData= data.getStringExtra("sortOrder","byName")
            sortYourDAta(sordData);
    }
}

}

并在活动b中选择排序顺序时

只将值存储在变量sortOrder =“byDate”;

和onbackpressed或者当你想要回去时只需要调用它 setResult(123,new Intent()。putExtra(“sortOrder”,sortOrder)); 光洁度();