0

我的代码在这里 请帮助解决这个问题 我正在制作一个应用程序,用户选择一些切换按钮,然后单击播放按钮

会发生什么应用程序记录所有的toggleButton id,当用户单击播放按钮时,应用程序会生成一个随机数,然后将其与选定的数字匹配,如果匹配则用户获胜......

游戏中切换按钮的总数设置为 12

请帮忙

我怎样才能获得选定的toggleButtons id以及如何将它们与随机数匹配请帮忙

主.java

package com.authorwjf;

import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Main extends Activity implements OnClickListener {

    private ArrayList<ToggleButton> mToggleButtons = new ArrayList<ToggleButton>();
    TextView textView1, textView2;
    Button button1;
    ToggleButton t;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textView1 = (TextView) findViewById(R.id.textView1);
        textView2 = (TextView) findViewById(R.id.textView2);
        button1 = (Button) findViewById(R.id.button1);

        ToggleButton cb = null;

        for (int i = 0; i < 12; i++) {
            cb = new ToggleButton(this);
            cb.setText(Integer.toString(i));
            cb.setTextOn(Integer.toString(i));
            cb.setTextOff(Integer.toString(i));
            cb.setBackgroundResource(R.drawable.check);
            cb.setOnClickListener(this);
            cb.setId(i);
            registerForContextMenu(cb);
            mToggleButtons.add(cb);
        }

        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new CustomAdapter(mToggleButtons));
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        t = (ToggleButton) v;
            if (t.isChecked()) {
            Toast.makeText(getBaseContext(), t.getText() + " Enable",
                    Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(getBaseContext(), t.getText() + " Disable",
                    Toast.LENGTH_SHORT).show();
        }
    }
}         

CustomAdapter.java

package com.authorwjf;

import java.util.ArrayList;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ToggleButton;

public class CustomAdapter extends BaseAdapter {

    private ArrayList<ToggleButton> mToggleButtons = null;

    public CustomAdapter(ArrayList<ToggleButton> b) 
    {
        mToggleButtons = b;
    }

    @Override
    public int getCount() {
        return mToggleButtons.size();
    }

    @Override
        public Object getItem(int position) {
        return (Object) mToggleButtons.get(position);
    }

    @Override
    public long getItemId(int position) {
        //in our case position and id are synonymous
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ToggleButton ToggleButton;
        if (convertView == null) {
            ToggleButton = mToggleButtons.get(position);
        } else {
            ToggleButton = (ToggleButton) convertView;
        }
        return ToggleButton;
        }

    }

请帮助堆栈

4

1 回答 1

1

为按钮添加一个点击监听器,在那里您应该能够获取在按下按钮时已检查的切换按钮的 ID:

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            textView1 = (TextView) findViewById(R.id.textView1);
            textView2 = (TextView) findViewById(R.id.textView2);
            button1 = (Button) findViewById(R.id.button1);

            ToggleButton cb = null;

            for (int i = 0; i < 12; i++) {
                cb = new ToggleButton(this);
                cb.setText(Integer.toString(i));
                cb.setTextOn(Integer.toString(i));
                cb.setTextOff(Integer.toString(i));
                cb.setBackgroundResource(R.drawable.check);
                cb.setOnClickListener(this);
                cb.setId(i);
                registerForContextMenu(cb);
                mToggleButtons.add(cb);
            }

            GridView gridView = (GridView) findViewById(R.id.gridview);
            gridView.setAdapter(new CustomAdapter(mToggleButtons));
            gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                }
            });

            button1.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {

                 int random = (new Random()).nextInt(12);

                 ArrayList<Integer> positive_IDs = new ArrayList<Integer>();
                 for(int i = 0; i < mToggleButtons; i++){
                     ToggleButton b = mToggleButtons.get(i);
                     if(b.isChecked()){
                    positive_IDs.add(b.getId());    
                     } 
                 }

                 // do whatever needed with the positive_IDs, for example:

                 if(positive_IDs.contains(random)){
                     // doAction();
                 }       
          }
           });


        }
于 2013-09-21T15:32:40.623 回答