我想为我的 Android 游戏创建一个“输入你的名字”页面(用于高分),但我遇到了一些问题。我希望它看起来像这样:
您已达到 (=enthst1)
......这是分数............
点!(=enthst2)
请输入您的姓名以保存您的分数!(= entst3)
...........名称的EditText......
后退(=按钮)...................... Enter(=按钮)
但我似乎无法将分数(int)添加到我的 ContentView!
这是代码:
“onCreate”中的 Java:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_hs);
layo = new RelativeLayout(this);
MySco = new TextView(this) ;
Back = (Button)findViewById(R.id.enthsre1);
Back.setOnClickListener(this);
Enter =(Button)findViewById(R.id.enthsok1);
Enter.setOnClickListener(this);
Eingabe = (EditText) findViewById(R.id.editText1) ;
Texta = (TextView) findViewById(R.id.enthst1) ;
Textb = (TextView) findViewById(R.id.enthst2) ;
Textc = (TextView) findViewById(R.id.enthst3) ;
Intent intentk = getIntent();
kontro = intentk.getStringExtra("from").equals("MainActivity") ;
score = 0 ;
if(kontro == false){
score = punkteRechnen(tuleb, le0leb, le1leb, le2leb) ; //calculate the score
} else {
score = 10 ;
}
scoint = "" + score ;
MySco.setText(scoint) ;
MySco.setTextColor(Color.WHITE) ;
MySco.setTextSize(20);
/*I know that this will throw me an IllegalStateException (the specified child already has a parent)
layo.addView(Texta) ;
layo.addView(MySco) ;
layo.addView(Textb) ;
layo.addView(Textc) ;
layo.addView(Eingabe) ;
layo.addView(Back) ;
layo.addView(Enter) ;
*/
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:background="@color/black"
android:orientation="vertical"
tools:context=".EnterHSActivity" >
<TextView
android:id="@+id/enthst1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/enthst1a"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/enthst2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:text="@string/enthst1b"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/enthst3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_centerHorizontal="true"
android:text="@string/enthst1c"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="18sp" />
<Button
android:id="@+id/enthsre1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="100dp"
android:text="@string/retour" />
<Button
android:id="@+id/enthsok1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="100dp"
android:text="@string/allesklar" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/enthsre1"
android:layout_alignRight="@+id/enthsok1"
android:layout_below="@+id/enthst3"
android:ems="10"
android:inputType="text"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textColorLink="@color/red" >
<requestFocus />
</EditText>
</RelativeLayout>
那么,如何将分数添加到我的布局中?