在RadioButtonGroup

时间:2015-07-13 20:49:22

标签: android

我有工具栏,其中有一个菜单。在该菜单中,我有很多选项,其中之一,当按下时打开一个带有RadioButtonGroup(带有setSingleChoiceItems)的alertDialog,用户可以在其中选择一个属性。在用户选择该媒体资源并点击AlertDialog的“确定”按钮后,我将返回后台的Activity

问题是,在用户选择alertDialog中的属性并单击确定按钮(alertDialog正常消失)后,我的活动的textViews(背景中的)应该更改(根据所选的属性)用户),他们没有改变。

我的所有活动(可以在用户选择alertdialog中的属性时在后台运行)扩展了我对工具栏的所有事件的活动(包括选择该属性,共享应用程序等)。我的意思是我有一个与工具栏菜单相关的所有事情的活动。每次用户选择不同的属性时,我都会将其保存在数据库中,然后我就可以阅读了。

如果用户选择该属性的值,我的后台活动的texviews会发生变化,该怎么办? (我想onResume很容易做到,但它不起作用。)感谢

请检查屏幕截图是否有助于您了解我的问题。红色是我想要改变的。 enter image description here

2 个答案:

答案 0 :(得分:0)

I don't think a dialog changes the activity stack, meaning onPause/onResume are not called.

As @sahu commented you can determine that your dialog closed in the positive/negative buttons:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do it?")
      .setPositiveButton("Yes", new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface dialog, int id)
        {
          dialog.dismiss();
          // change your textviews
        }
      })
      .setNegativeButton("No", new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface dialog, int id)
        {
          dialog.dismiss();
          // cancel pressed, probably don't need to do anything
        }
      });
    builder.create().show();
}

EDIT:

If your dialog code is in a different class you will need to add an interface to communicate from the toolbar back to your activity. This is not much different than fragment to activity communication:

http://developer.android.com/training/basics/fragments/communicating.html

In your toolbar you will need to define an interface that your activity can call.

public class MyToolbar extends Toolbar {
    OnInfoChangedListener mCallback;

    // Don't forget to call this from your activity to setup the listener
    public setOnInfoChangedListener(OnInfoChangedListener callback) {
      mCallback = callback;
    }

    // Container Activity must implement this interface
    public interface OnInfoChangedListener {
        // you can add arguments here as needed
        public void onInfoChanged();
    }
    ...

}

Then in your setPositive button tell your activity that the info changed:

  .setPositiveButton("Yes", new DialogInterface.OnClickListener()
  {
    public void onClick(DialogInterface dialog, int id)
    {
      dialog.dismiss();
      if (mCallback != null) mCallback.onInfoChanged();
      // change your textviews
    }
  })

Then in your activity:

public static class MainActivity extends Activity
        implements OnInfoChangedListener {
    ...

        @Override
        public void onCreate(bundle) {
            super.onCreate(bundle);
            Toolbar toolbar = // get the toolbar;
            toolbar.setOnInfoChangedListener(this);  // set myself as listener
        }

        public void onInfoChanged() {
            // The user selected something from your toolbar 
            // update your views
        }
}

You can define your interface to pass whatever you want from your toolbar into your activity, this is just a simple 'hey something changed notification'.

答案 1 :(得分:0)

try

       radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
       {

        public void onCheckedChanged(RadioGroup group, int checkedId) 
           {
             switch(checkID)
             case 0: textview.setText("your text");
              break;
              ...

it may help !