![](/img/trans.png)
[英]How can I change the background color of my MainActivity from a second activity using colored buttons as user choices?
[英]How to change text in second_xml from mainActivity
I am developing the following application in android Studio-
**user on entering todo and click add button, it Should add to linear layout with checkbox which
is helpful to delete todos.**
my idea: I created a new XML file with textView and CheckBox in single_item.xml and inflate it to
linearlayout which is present in main xml as shown in
below.............................................
problem: when I clicked the add button , app crashes.
I found that this issue is because I added textview.setText(text) in java code.
if I had not added this line , then sample single_element.xml is getting added
LinearLayout.But I needed to content of textview to be set to input
that is given by user in editText as shown.
I had deleted Most of the code in xml file because Stackoverflow is showing me "It looks like your post is mostly code; please add some more details." ""...............................................
..................................................................................................................................................................................................................................................................................................................................................................................
[activity_main.xml][1]
[single_element.xml][2]
[1]: https://i.stack.imgur.com/UhtGJ.png
[2]: https://i.stack.imgur.com/Pcu1O.png
在activity_main.xml我有 LinearLayout、editText 視圖、添加按鈕和刪除按鈕。
LinearLayout -> id="@+id/linearLayout"
EditText -> android:id="@+id/edit"
<Button ->android:id="@+id/add_button"
single_element.xml
TextView ->id="@+id/textView"
CheckBox ->id="@+id/checkBox"
MainActivity.java
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
EditText editText = (EditText) findViewById(R.id.edit);
String text = editText.getText().toString().trim();
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(text);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.single_element,null);
linearLayout.addView(view);
}
});
提前致謝............................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ...................................................
您必須將文本框和復選框都放在 activity_main.xml 中,無需創建另一個布局文件(single_element.xml)來添加它們,因為您需要在 MainActivity class 中顯示文本和復選框。 否則,您必須為 single_element.xml 創建另一個活動,例如可以稱為 SingleElementActivity,然后使用意圖將在 EditText 中輸入的數據從 MainActivity 傳遞到此 SingleElementActivity。
請參閱此處如何使用 Intent 將數據從活動傳遞到另一個活動: 如何在 Android 應用程序中的活動之間傳遞數據?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.