ANDROID:点击一个按钮后添加复选框

问题描述:

我试图做一个应用程序,当我点击一个按钮时添加新的复选框。 此外,新的复选框必须从我已添加的texteditor中取得名称。 任何人都可以帮助我吗?ANDROID:点击一个按钮后添加复选框

这是我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" /> 

+0

你有什么问题?你需要更具体的问题是什么,否则人们很可能不会尝试和帮你 – Zyber 2013-05-02 10:13:32

+0

如果你有修复复选框...然后使用可见性.. – 2013-05-02 10:51:39

+0

你的问题不是很清楚。你必须简要解释你现在面临的任何问题。尝试更新它。 – Dhasneem 2013-05-02 11:08:01

您可以添加到您的布局已经充气之后编程。一个ID添加到您的LinearLayout:

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

您实际上需要将该代码移动到您的按钮的点击事件处理程序当然。如果你想能够像这样动态添加多个复选框,我想你需要在一个数组中管理它们。你在哪里说“复选框必须从文本编辑器中取名”我假设这是对复选框显示的文本,在这种情况下,您需要同时创建一个TextView,并从EditText中设置其文本。

那么在配置更改(即设备方向更改)时会发生什么问题。这会重新创建活动,因此除非将它们存储在onSaveInstanceState()中,然后使用SharedPreferences将它们还原到onRestoreInstanceState()中,否则动态添加将会丢失。

我希望能帮助你开始。