我有一个 Spinner、EditText 和一个按钮。当我在 EditText 中输入内容然后单击按钮时,我希望它根据选择的微调器上的项目创建一个新的 TextView。
按钮点击方法:
public void submitScore(View v){
final int position = playerList.getSelectedItemPosition();
EditText input = (EditText) findViewById(R.id.editText1);
createNewTextView (players.get(position) + input);
}
创建NewTextView():
private TextView createNewTextView (String text){
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
newTextView.setLayoutParams(lparams);
newTextView.setText(text);
return newTextView;
}
整个 XML 代码:
<RelativeLayout 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"
tools:context=".Igra" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:orientation="vertical" >
</LinearLayout>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_toLeftOf="@+id/button1"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:text="Enter"
android:onClick="submitScore" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="Score:" />
如你所见;我在此 Activity 中同时拥有相对布局和线性布局,并且它们是分开的(线性布局位于相对布局下)。
我希望将此 TextView 添加到 LinearLayout 中,但它没有添加到任何地方,当我按下按钮时我看不到它。
我现在的代码:
public void submitScore(View v){
LinearLayout lLayout = (LinearLayout) findViewById (R.id.linearLayout);
final int position = playerList.getSelectedItemPosition();
EditText input = (EditText) findViewById(R.id.editText1);
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
newTextView.setLayoutParams(lparams);
newTextView.setText(players.get(position) + input);
//createNewTextView (players.get(position) + input);
lLayout.addView(newTextView);
}
结果:
最佳答案
在 submitScore()
上,您正在创建一个 TextView
,但没有将其添加到 LinearLayout
中。
给您 LinearLayout
一个 android:id
值,然后在 submitScore()
上获取其引用并调用 add(
方法添加从 LinearLayout
的 )createNewTextView ()
返回的值
关于java - 创建新的 TextView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18476712/