0

在我的代码中,如果我们需要特定数量的复选框或单选按钮,通过在 EditText 中给出计数值,我们可以获得特定数量的复选框。但它显示的复选框带有从 a 到 z 的随机字母。但是,我需要它根据我的需要专门更改/重命名。非常感谢您的帮助。提前致谢。这是我的代码。

XML 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity" >

    <EditText
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="05dp"
            android:hint="Enter Text" />

    <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_alignParentRight="true"
            android:layout_marginRight="05dp"
            android:text="Edit Text" />

    <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/button2"
            android:text="Check Box" />

    <Button
            android:id="@+id/button5"
            android:layout_width="98dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/button4"
            android:text="Radio Button" />


    <EditText
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button4"
            android:layout_alignParentLeft="true"
            android:hint="Enter no" />


    <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button5"
            android:gravity="left"
            android:orientation="vertical" />

    <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayout"
            android:layout_centerHorizontal="true"
            android:orientation="vertical" />

</RelativeLayout>

主要活动

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.LinearLayout.*;

import java.util.Random;

public class MainActivity extends Activity {
    private LinearLayout mLayout;
    private EditText mEditText;
    private Button mButton;
    Button abutton;


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

        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mEditText = (EditText) findViewById(R.id.button1);
        mButton = (Button) findViewById(R.id.button2);
        mButton.setOnClickListener(onClick());
        TextView textView = new TextView(this);
        textView.setText("New text");


        final EditText button2=(EditText)findViewById(R.id.button3);

        findViewById(R.id.button5).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                int number=Integer.parseInt(button2.getText().toString());
                addRadioButtons(number);
            }
        });

        findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                int number=Integer.parseInt(button2.getText().toString());
                addCheckBox(number);
            }
        });
    }
    public void addRadioButtons(int number) {

        for (int row = 0; row < 1; row++) {
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);

            for (int i = 1; i <= number; i++) {
                RadioButton rdbtn = new RadioButton(this);
                rdbtn.setId((row * 2) + i);
                rdbtn.setText("Radio " + rdbtn.getId());
                ll.addView(rdbtn);
            }
            ((ViewGroup) findViewById(R.id.radiogroup)).addView(ll);
        }

    }

    public void addCheckBox(int number) {

        //Edited Here
        String[] names = {"Sanket", "Kumar", "Rahul"};

        for (int row = 0; row < 1; row++) {
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);

            for (int i = 1; i <= number; i++) {
                CheckBox ch = new CheckBox(this);
                ch.setId((row * 2) + i);

                //ch.setText(randomString(3));

                ch.setText(names[i]);
                ll.addView(ch);
            }
            ((ViewGroup) findViewById(R.id.radiogroup)).addView(ll);
        }


    }
    private String randomString(int len) {
        char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
        StringBuilder sb = new StringBuilder(len);
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            char c = chars[random.nextInt(chars.length)];
            sb.append(c);
        }
        String output = sb.toString();
        System.out.println(output);
        return sb.toString();
    }
    private OnClickListener onClick() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                mLayout.addView(createNewTextView(mEditText.getText().toString()));
            }
        };
    }

    private TextView createNewTextView(String text) {
        final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setText("" + text);
        return textView;
    }


}
4

6 回答 6

0

在您的 addCheckBox() 方法中,您已将复选框的文本设置为 randomstring (ch.setText(randomString(3)))。只需将此行更改为您的选择,就像这个ch.setText("Checkbox" + i)

于 2013-10-17T11:27:12.590 回答
0

我认为您应该尝试使用 RadioGroup 并创建一个字符串数组,使用该数组可以轻松使用 .setText 方法通过所有单选按钮运行循环。

于 2014-08-21T09:05:47.127 回答
0
for (int i = 1; i <= number; i++) {
                CheckBox ch = new CheckBox(this);
                ch.setId((row * 2) + i);
                ch.setText(randomString(3)); // Change Here
                ll.addView(ch);
            }

更改您想要的 ch.setText 中 checkBox 的文本

于 2013-10-17T11:29:58.633 回答
0

制作一个类似的字符串数组

String[] names = {"Sanket", "Kumar", ......};

然后在代码中

for (int i = 1; i <= number; i++) {
                CheckBox ch = new CheckBox(this);
                ch.setId((row * 2) + i);

                // Replace the line with
                //ch.setText(randomString(3)); 

                // with this line
                ch.setText(names[i]);  

                ll.addView(ch);
            }
于 2013-10-17T11:39:47.920 回答
0

好的..我真的不明白...当您创建复选框时,您将其文本设置为 ch.setText(randomString(3)); 如果您想要特定的文本,为什么要这样做?

随机!=具体。

写这样的东西:

ch.setText("Your desired text");

或者你可以创建一个

String[] checkBoxFruits= {"apple","mango","kiwi"};

然后将文本设置为:

for ( int i=0; i<numberOfCheckBoxes; i++ )
{
 CheckBox ch = new CheckBox(this);
 ch.setId((row * 2) + i);
 ch.setText(checkBoxFruits[i]);
 ll.addView(ch);
}
于 2013-10-17T11:34:31.497 回答
0
public void addCheckBox(int number) {

    String[] names = {"Sanket", "Kumar", "Eagle"};
    for (int row = 0; row < 1; row++) {
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);

        for (int i = 1; i <= number; i++) {
            CheckBox ch = new CheckBox(this);
            ch.setId((row * 2) + i);
            if(i != names.length)
               ch.setText(names[i-1]);
            else
               ch.setText("New name");
            ll.addView(ch);
        }
        ((ViewGroup) findViewById(R.id.radiogroup)).addView(ll);
    }


}

使用上面的代码..并检查它...

于 2013-10-17T11:50:42.713 回答