2013-05-02 39 views
0

我試圖做一個應用程序,當我點擊一個按鈕時添加新的複選框。 此外,新的複選框必須從我已添加的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

回答

0

您可以添加到您的佈局已經充氣之後編程。一個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()中,否則動態添加將會丟失。

我希望能幫助你開始。

相關問題