我有问题无法在UITextView中设置文本。请参阅下面的代码,在setText()之后TwxtView值不会改变。当我按下按钮“hello word”不改变。 当我按下按钮时,我想将“你好世界”改为“你好吗”。
MAinActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showPopup(View view){
final TextView hello = (TextView)findViewById(R.id.textView1);
System.out.println(hello.getText().toString());
String how = "how are you";
hello.setText(how);
System.out.println(hello.getText().toString());
setContentView(R.layout.activity_main);
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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="com.example.popup.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:onClick="showPopup"
android:text="Button" />
</RelativeLayout>
答案 0 :(得分:0)
从setContentView
移除showPopup
。这会将布局和文本视图重置为原始文本。
答案 1 :(得分:0)
您无需将视图设置两次。使用以下代码。
public class MainActivity extends ActionBarActivity {
TextView hello;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hello = (TextView)findViewById(R.id.textView1);
}
public void showPopup(View view){
String how = "How are you";
hello.setText(how);
}
}