2015-11-21 33 views
1

我是新的Java和Android編程。我想要做的是創建一個應用程序,在EditText字段中引入名稱後創建一個複選框。就像創建待辦事項列表或購物清單一樣。Android應用程序。從文本字段創建複選框

public class MainActivity extends AppCompatActivity { 

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

public void storeProduct(View view) { 
    //Input name 
    EditText productField = (EditText) findViewById(R.id.product_field); 
    String product = productField.getText().toString(); 
} 

public void createCheckBox(String product){ 
    //Create Checkbox 
    CheckBox savedPRoduct = new CheckBox(getApplicationContext()); 
    savedProduct.setText(producto); 
} 

這就是我到目前爲止。我不確定它是否正確。我一直在討論文檔和一些論壇,包括這個,我設法創建了方法storeProduct來保存EditText視圖中的字符串。然後,我創建createCheckbox方法,該方法使用EditText視圖中的字符串生成checbox。我不確定的是創建顯示創建的複選框的方法。

任何線索和信息,你可以與我分享真的很感激。

非常感謝。

回答

0

您通常需要添加視圖內容位於下RES /佈局在文件/ R.layout.activity_main.xml

從那裏,你可以添加你在你的佈局需要的所有視圖。這裏是android文檔的一個示例佈局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="16dp" 
android:paddingRight="16dp" 
android:orientation="vertical" > 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/to" /> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/subject" /> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:gravity="top" 
    android:hint="@string/message" /> 
<Button 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:text="@string/send" /> 
</LinearLayout> 

你也可以通過編程的方式來完成,但我不會建議你這樣做,因爲它違背了Android的良好做法。

+0

那麼,只有當storeProduct按鈕被按下時,應用程序纔會創建複選框。我在XML文件中只有EditText視圖和Button視圖。你能告訴我爲什麼它違背了Android的良好做法嗎? – Poleas

+0

你會結束ip更復雜的代碼,當它不是99%的時間需要它。關於你的問題,你可以在按下按鈕之前設置複選框的屬性android:visibility =「gone」。您可以將視圖的可見性設置爲可見。 –

+0

太好了,我可以創建一個複選框。現在我將研究一些想到的循環,每當我按下按鈕時創建不同的複選框:) – Poleas

0

因此,與KevDev的幫助下,我設法想出這個XML代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 


<EditText 
    android:id="@+id/product_field" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textCapSentences" 
    android:hint="Product"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="storeProduct" 
    android:text="Guardar"/> 

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="product" 
    android:id="@+id/productCheckBox" 
    android:visibility="gone"/> 
</LinearLayout> 

這java代碼:

public class MainActivity extends AppCompatActivity { 

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

    public void storeProduct(View view) { 
     //Input name 
     EditText productField = (EditText) findViewById(R.id.product_field); 
     String product = productField.getText().toString(); 

     CheckBox savedProduct = (CheckBox) findViewById(R.id.productCheckBox); 
     savedProduct.setText(product); 
     savedProduct.setVisibility(View.VISIBLE); 
    } 


} 

這隻能說明一個複選框一次,但我m正在努力製作一個循環來放置一系列複選框。我還正在查看recyclerview文檔,以在此代碼中介紹它。