如何从实用的textview中用逗号分隔的字符串制作按钮。
TextView ingredient_txtview = (TextView) findViewById(R.id.recipe);
String ingredients = ingredient_txtview.getText();
String[] btnArray = ingredients.split(",");
for(int i=0;i<btnArray.length;i++){
// something
}
示例:
[Textview]“ Button1,Button2,Button3”
结果:
[Button] Button1
[Button] Button2
[Button] Button3
答案 0 :(得分:0)
首先,您必须创建布局(记住id
od根元素):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
接下来,您可以在“活动”(例如Kotlin中的示例)中使用此布局:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
var listOfButtons: MutableList<Button> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val input = "Button1, Button2, Button3"
val split = input.split(", ")
for (text in split) {
val button = Button(this)
button.text = text
linear_layout.addView(button)
listOfButtons.add(button)
}
}
}
答案 1 :(得分:0)
List<Button> btns = new ArrayList();
for (String name: txtView.getText().split(",")){
Button b = new Button(context);
b.setText(name);
btns.add(b);
}