我知道很多人都问过这个问题,但我的问题有所不同:
我已经有一个 xml 布局(使用选项卡),但我需要根据对字符串的读取来创建按钮。
例如: 查询返回四个词。我需要创建四个按钮,单击它们时会出现带有单词的 toast 词。
注意:不知道能找到多少字。
代码看起来像这样:
while (Content.indexOf("<glossary>") != -1) { Content = Content.substring(Content.indexOf("<glossary>") + 9); //create button with name "Content" }
编辑:
看看我的新代码,只创建一个按钮:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showphyto);
intent = getIntent();
Title = intent.getStringExtra("Title");
Content = intent.getStringExtra("Content");
setTitle(Title);
//if(getIntent().getData().getQueryParameter("author") != null)
TabSpec descritor;
/************************** Tab General **************************/
descritor = getTabHost().newTabSpec("tag1");
descritor.setContent(R.id.General);
Button myButton = new Button(ShowAllegationActivity.this);
myButton.setText("test");
LinearLayout myContainer = (LinearLayout) findViewById(R.id.General);
myContainer.addView(myButton);
descritor.setIndicator("General", getResources().getDrawable(R.drawable.icon));
getTabHost().addTab(descritor);
/************************** Tab 2 **************************/
descritor = getTabHost().newTabSpec("tag2");
descritor.setContent(R.id.Recipe);
Teste = (TextView) findViewById(R.id.teste2);
Teste.setText("Teste2");
descritor.setIndicator("Tab2", getResources().getDrawable(R.drawable.icon));
getTabHost().addTab(descritor);
/************************** Tab3 3 **************************/
descritor = getTabHost().newTabSpec("tag3");
descritor.setContent(R.id.Related);
Teste = (TextView) findViewById(R.id.teste3);
Teste.setText("Teste3");
descritor.setIndicator("Tab3", getResources().getDrawable(R.drawable.icon));
getTabHost().addTab(descritor);
getTabHost().setCurrentTab(0);
}
xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/General">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/teste" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/General2">
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Recipe">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/teste2" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/Related">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/teste3" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
最佳答案
您可以在代码中创建一个按钮:
Button myButton = new Button();
myButton.setLayoutParams(myLayoutParams);
myButton.setText(myText);
要让它显示 Toast,添加一个 onClickListener:
myButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
//The parameter view is the button that was clicked
Toast.makeText(context, ((Button) view).getText(), Toast.LENGTH_LONG).show()
//TypeCasting to Button is important, because a normal View doesn't have a getText method
}
});
然后,要将它们添加到您的屏幕,您需要在 xml 中定义一个容器来容纳它们,例如 LinearLayout。获取 LinearLayout:
LinearLayout myContainer = findViewById(R.id.myButtonId); //Make sure you set the android:id attribute in xml
您可能希望这段代码在您的循环之外,也许就在它之前,这样您就不会在每次迭代时都获得布局。
然后,在循环中,在设置按钮之后:
myContainer.addView(myButton);
最后,关于 myLayoutParams
变量的说明。当您为 View 设置参数时,它们需要对应于您的小部件的父 View 。因此,如果您将 Button 放在 LinearLayout 中,它看起来像
//This will make LayoutParameters with layout_width="wrap_content" and layout_height="fill_parent"
LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT);
您也可以将它放在循环之外,并为每个 Button 重用它。
关于android - 如何动态创建一个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10557530/