1

我在我的 xml 布局中定义了一个 textview。现在,当我想在我的活动 Java 文件中调用这个 textview 时,我不知道该怎么做。你能告诉我怎么做吗?提前谢谢。

这是我的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/bubble_sort"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="left" 
    android:text=
"hello welome to your first c++ code."/>

</LinearLayout>

这是我的 Java 文件:

package com.example.cprograms;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Bubble_sort extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        TextView tv = (TextView) findViewById(R.id.bubble_sort);
    }
}
4

3 回答 3

1

添加这些行

setcontentView(R.layout.yourxmlfile);

tv.setText("Hello")

......

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setcontentView(R.layout.yourxmlfile);
    TextView tv = (TextView) findViewById(R.id.bubble_sort);
     tv.setText("Hello");

}
于 2013-02-01T09:13:06.917 回答
0
package com.example.cprograms;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Bubble_sort extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setcontentView(R.layout.YOUR_MAIN_ACTIVITY.XML)
    TextView tv = (TextView) findViewById(R.id.bubble_sort);
    tv.setText("HelloWorld"); //Here Setting Text During Run time.

}

}
于 2013-02-01T09:17:14.540 回答
0

您尚未setcontentView(R.layout.yourXmlFileName) 在 Activity 中调用方法。之后调用此方法super.onCreate(savedInstanceState)

希望这可以帮助。

于 2013-02-01T09:51:08.340 回答