我有一个 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);
}
创建新文本视图():
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:" />
如你看到的; 我在这个活动中既有相对布局也有线性布局,它们是分开的(线性布局在相对布局下)。
我希望将此 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);
}
结果: