【问题标题】:How to create Alertbox from dynamic Checkbox in Android?如何从 Android 中的动态复选框创建警报框?
【发布时间】:2014-04-11 02:23:23
【问题描述】:

我为我的应用创建了六个动态 Checkbox,如果复选框被选中,我想 Pop-up 一个 AlertBox 但我我从我的代码中找到任何结果。我是复选框的真正价值,但没有 AlertBox 即将到来。

Java 代码:

package com.example.mycheckbox;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.SparseBooleanArray;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout ll = (LinearLayout) findViewById(R.id.ii);
        CheckBox[] cb = new CheckBox[6];

        for (int i = 0; i < 6; i++) 
        {
            cb[i] = new CheckBox(this);
            cb[i].setText("Dynamic Checkbox " + i);
            cb[i].setId(i + 6);

            if (cb[i].isChecked())
            {
                AlertDialog.Builder myalert = new AlertDialog.Builder(this);

                myalert.setTitle("Demo Title - "+i).setMessage(" Message from - "+i).setNeutralButton("Close", null).show();
            }

            ll.addView(cb[i]);                       
        }
    }
}

如果选中,我想为每个CheckBox 弹出一个AlertDialog

【问题讨论】:

    标签: android checkbox android-alertdialog


    【解决方案1】:

    也许这会对你有所帮助。Checkbox Listener。在创建复选框时添加它们。或者参考你正在设置的id创建的复选框,然后设置一个监听器。

    【讨论】:

      【解决方案2】:

      AlertDialog 未显示,因为您没有从 AlertDialog.Builder 创建 AlertDialog 对象。如下创建 ab AlertDialog 对象,然后使用该对象调用 show() 方法以显示 Dialog

      另外,您没有将Listener 分配给CheckBox。现在将侦听器设置为它们并显示如下对话框...

      cb[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      
         @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      
              AlertDialog.Builder myalert = new AlertDialog.Builder(this);
      
              myalert.setTitle("Demo Title - "+i);
              myalert.setMessage(" Message from - "+i);
              myalert.setNeutralButton("Close", null);
      
              AlertDialog dialog = myalert.create();
              dialog.show();
         }
      }
      

      【讨论】:

      • 没有朋友,没有显示任何警告框......我认为你的代码非常完美但没有得到任何结果......
      • 是的,得到警报框,但它显示了每个警报框的最后一个索引值,所以我为每个警报框得到的结果是 Demo Title - 6 , Message from - 6 ....
      • 拜托,你自己试试吧……我得走了,因为这里深夜……如果你没有得到任何解决方案,那么我会在早上检查它……祝你好运...如果它有帮助,那么不要忘记接受它。
      【解决方案3】:

      默认情况下,它们不会被检查,因此不会显示任何弹出窗口,如果有人检查或不检查,则应添加侦听器。

      cb[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {
      
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // alert dialog 
      
            }
      });
      

      或者您可以实现 OnCheckedChangeListener 并为所有复选框使用一个侦听器。

      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          //
      
      }
      

      【讨论】:

      • No Man getting ArrayIndexOutOfBoundsException ... length=6 , index=6 error .... 我不知道它是怎么来的??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多