ANDROID:单击按钮后添加复选框

时间:2013-05-02 09:53:04

标签: android android-checkbox

我正在尝试执行一个应用程序,只要我点击一个按钮,就会为我添加新的复选框。 此外,新复选框必须从我已经添加的texteditor中取名。 有谁可以帮助我?

这是我的MyAndroidAppActivity

 package com.example.myandroidapp;
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.CheckBox;    
 import android.widget.Toast;


public class MyAndroidAppActivity extends Activity {

private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay, btn_add;
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";


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

}



public void addListenerOnChkIos() {

    chkIos = (CheckBox) findViewById(R.id.chkIos);

    chkIos.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (((CheckBox) v).isChecked()) {
                Toast.makeText(MyAndroidAppActivity.this,
                        "Bro, try Android :)", Toast.LENGTH_LONG).show();
            }

        }
    });

}

public void addListenerOnButton() {

    chkIos = (CheckBox) findViewById(R.id.chkIos);
    chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
    chkWindows = (CheckBox) findViewById(R.id.chkWindows);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

    btnDisplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            StringBuffer result = new StringBuffer();
            result.append("IPhone check : ")
                    .append(chkIos.isChecked());
            result.append("\nAndroid check : ").append(
                    chkAndroid.isChecked());
            result.append("\nWindows Mobile check :").append(
                    chkWindows.isChecked());

            Toast.makeText(MyAndroidAppActivity.this, result.toString(),
                    Toast.LENGTH_LONG).show();

        }
    });

}

}

这是main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent">



  <EditText

   android:id="@+id/edit_message"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:ems="10"
   android:hint="@string/edit_message" />

  <requestFocus />




  <Button
      android:id="@+id/btn_add"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/button_add" />

<CheckBox
    android:id="@+id/chkIos"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/chk_ios" />

<CheckBox
    android:id="@+id/chkAndroid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="@string/chk_android" />

<CheckBox
    android:id="@+id/chkWindows"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/chk_windows" />

<Button
    android:id="@+id/btnDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_display" />  

1 个答案:

答案 0 :(得分:0)

您可以在充气后以编程方式添加到布局中。在您的LinearLayout中添加一个ID:

android:id ="@+id/layout"

然后在OnCreate中(在现有代码之后),获取对它的引用并根据需要添加控件。例如:

LinearLayout ll = (LinearLayout)findViewById(R.id.layout);

CheckBox cb = new CheckBox(this);
int lHeight = LinearLayout.LayoutParams.MATCH_PARENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;

ll.addView(cb, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(ll);

您实际上需要将该代码移动到按钮的click事件处理程序中。如果您希望能够像这样动态添加多个复选框,我认为您需要在数组中管理它们。如果你说“复选框必须从texteditor中取名”,我认为这是显示在复选框上的文本,在这种情况下,你需要同时创建一个TextView并从EditText设置它的文本。

然后存在配置改变(即设备方向的改变)发生的问题。这会重新创建活动,因此除非您将它们存储在onSaveInstanceState()中,然后使用SharedPreferences在onRestoreInstanceState()中恢复它们,否则您的动态添加将会丢失。

我希望这有助于您入门。

相关问题