在Android中的切换按钮中从一个状态切换到关闭状态时显示警报

时间:2012-12-10 07:11:02

标签: android

当我在切换按钮中从开启状态切换到关闭状态时,我试图显示警报。我在侧列表视图中使用适配器中的切换按钮。请参考下面的代码。

当用户在切换按钮中从开状态切换到关状态时,我也想显示警报。当我使用IsChecked()时,它总是被打开到关闭或关闭到n,但我只需要在从开启切换到关闭时显示警报。

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >

<TextView
    android:id="@+id/state_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ToggleButton
    android:id="@+id/statetoggleButton"
    style="@style/on_off_togle_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/togllebuttonselector" />

适配器类是:

private final Activity context;
    public final List<StatesModel> list;
    AlertDialog alertDialog;

    public List<StatesModel> getList() {
        return list;
    }

    public StateListAdapter(Activity context, List<StatesModel> list) {
        super(context, R.layout.states_list, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected ToggleButton checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.states_list, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.state_name);
            viewHolder.checkbox = (ToggleButton) view
                    .findViewById(R.id.statetoggleButton);
            viewHolder.checkbox
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(
                            final CompoundButton buttonView,
                            boolean isChecked) {
                        final StatesModel element = (StatesModel) viewHolder.checkbox
                                .getTag();

                          if (!isChecked) { AlertDialog.Builder builder =
                          new AlertDialog.Builder( context);
                          builder.setTitle("Warning"); String message =
                          "Title is here"
                          ; builder.setMessage(message);
                          builder.setPositiveButton("OK", new
                          DialogInterface.OnClickListener() {

                          @Override public void onClick( DialogInterface
                          dialog, int which) { alertDialog.dismiss();
                          element.setSelected(buttonView .isChecked()); }
                          }); builder.setNegativeButton("Cancel", new
                          DialogInterface.OnClickListener() {

                          @Override public void onClick( DialogInterface
                          dialog, int which) { buttonView.setChecked(true);
                          alertDialog.dismiss(); } }); alertDialog =
                          builder.create(); alertDialog.show(); } else





                        element.setSelected(buttonView.isChecked());

                    }
                });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }

请检查检查更改方法。

1 个答案:

答案 0 :(得分:0)

ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // The toggle is enabled
        } else {
            // The toggle is disabled
        }
    }
});
  

如果检查是否写,否则不使用checke代码