0

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

当用户在 Toggle 按钮中从 On 状态切换到 Off 状态时,我还想显示警报。当我使用 IsChecked() 时,它总是被称为 on 到 off 或 off 到 n,但我需要在仅从 On 切换到 off 时显示 Alert。

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;
    }

请检查 On Checked 更改方法。

4

1 回答 1

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
        }
    }
});

if is check do write else not checke 使用的代码

于 2012-12-10T07:16:59.567 回答