2013-05-02 5 views
0

버튼을 클릭 할 때마다 새로운 체크 박스를 추가하는 애플리케이션을 만들려고합니다. 또한 새 체크 박스는 이미 추가 한 텍스트 편집기에서 이름을 가져와야합니다. 누구도 도와 줄 수 있습니까?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

+0

수정 체크 박스가있는 경우 ... 가시성을 사용하십시오. –

+0

질문이 명확하지 않습니다. 지금 당면하고있는 문제에 대해 간단히 설명해야합니다. 업데이트를 시도하십시오. – Dhasneem

답변

0

비정상적으로 레이아웃을 프로그래밍 방식으로 추가 할 수 있습니다. 당신의 LinearLayout에 ID를 추가 : 당신이 원하는대로 (기존 코드 후)

에서 OnCreate에서 다음
android:id ="@+id/layout" 

,에 대한 참조를 얻을 수 및 컨트롤을 추가 할 수 있습니다. 예 :

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를 함께 만들고 편집 텍스트에서 텍스트를 설정해야합니다.

그런 다음 구성 변경 (즉, 장치 방향 변경)시 발생하는 문제가 있습니다. 그러면 해당 활동이 다시 작성되어 onSaveInstanceState()에 저장하고 SharedPreferences를 사용하여 onRestoreInstanceState()에 복원하지 않는 한 동적 추가가 손실됩니다.

시작 하시는데 도움이되기를 바랍니다.

관련 문제