2013-08-28 29 views
1

假設我有3個複選框,每個複選框都有相應的字符串值。我想要的是,當我檢查複選框1和2時,它們相應的字符串或值將全部放在edittext上。使用複選框。複選框字符串值將在選中時傳輸到edittext

Example: 
[✓] Checkbox 1 (Egg) 
[✓] Checkbox 2 (Hotdog) 
[ ] Checkbox 3 (Cheese) 

當我檢查方框1和2時,它將編輯文本並查看爲「蛋」\ n +「熱狗」或類似內容。 輸出:

EDITTEXT:

Egg 
Hotdog 

這可能嗎?謝謝你的幫助!

+0

不管是什麼我敢肯定,這是可能的。我不完全確定你的意思。 – Cruncher

+0

它是這樣的。我知道它簡單,但我不知道如何。 – Kerv

+0

@Cruncher複選框1,2和3具有特定的值。它就像一個訂購系統。如果我選擇雞蛋和熱狗,並點擊「保存」按鈕,檢查項目的所有值將被轉移到編輯文本中。 – Kerv

回答

2
 package com.example.checkboxes; 

     import android.app.Activity; 
     import android.os.Bundle; 
     import android.view.Menu; 
     import android.view.View; 
     import android.view.View.OnClickListener; 
     import android.widget.CheckBox; 
     import android.widget.EditText; 
     import android.widget.Toast; 

     public class MainActivity extends Activity { 
    private CheckBox egg, hotdog, cheese; 
     private OnClickListener checkboxclicklistener; 
     private EditText display; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    egg = (CheckBox) findViewById(R.id.a); 
    cheese = (CheckBox) findViewById(R.id.c); 
    hotdog = (CheckBox) findViewById(R.id.b);  
    display = (EditText) findViewById(R.id.display); 


    createListener(egg); 
    createListener(hotdog); 
    createListener(cheese); 



} 

public void createListener(CheckBox checkbox) { 



     checkbox.setOnClickListener(new OnClickListener() { 
      StringBuilder checkeditems = new StringBuilder(); 
      @Override 
      public void onClick(View v) { 
       //is checkbox checked? 


       if (((CheckBox) v).isChecked()) { 

        String checkboxname = ((CheckBox) v).getText().toString(); 

        Toast.makeText(getApplicationContext(), checkboxname, Toast.LENGTH_LONG).show(); 

        if(checkboxname.equalsIgnoreCase("egg")) 
        { 
         display.setText(display.getText().toString()+"\nEgg"); 
        } 

        else if(checkboxname.equalsIgnoreCase("hotdog")) 
        { 
         display.setText(display.getText().toString()+"\nHotdog"); 
        } 

        else if(checkboxname.equalsIgnoreCase("cheese")) 
        { 
         display.setText(display.getText().toString()+"\nCheese"); 
        } 





       } 

       else if (((CheckBox) v).isChecked()==false) 
       { 
        String checkboxname = ((CheckBox) v).getText().toString(); 

        if(checkboxname.equalsIgnoreCase("egg")) 
        { 
         display.setText(display.getText().toString().replace("\nEgg", "")); 
        } 

        else if(checkboxname.equalsIgnoreCase("hotdog")) 
        { 
         display.setText(display.getText().toString().replace("\nHotdog", "")); 
        } 

        else if(checkboxname.equalsIgnoreCase("cheese")) 
        { 
         display.setText(display.getText().toString().replace("\nCheese", "")); 
        } 
       } 

       } 
      }); 


} 





@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

    } 
+0

我覺得你的onClick太複雜了。爲什麼你需要一個if語句來表示複選框的名字?您可以通過獲取複選框上的文本並附加該文件來完成此操作。 – Cruncher

+0

嘿,是的,你是對的!當我給出這個答案時,我很困 –

1

你可以使用setOnClickListener像這樣

CheckBox chkEgg; 
EditText yourEditText; 
String yourText; 
public void addListenerOnChkIos() { 

    chkEgg = (CheckBox) findViewById(R.id.checkbox1); 

    chkEgg.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
       //is chkEgg checked? 
     if (((CheckBox) v).isChecked()) { 
      yourText = yourText + "Egg"; 
      yourEditText.setText(yourText); 
     } 
    } 
    }); 

}

我只是做它一個 希望這有助於