android - 在您的 Activity 中定义 TextView

标签 android textview

我在我的 xml 布局中定义了一个 TextView 。现在,当我想在我的 Activity 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);
    }
}

最佳答案

添加这些行

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");

}

关于android - 在您的 Activity 中定义 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14642897/

相关文章:

android - 已编译的 apk 正在尝试访问 metor 服务器 react native

android - android.os.Handler 和 java.util.logging.Handler 之间的区别?

android - 为什么层次查看器不适用于三星 Galaxy TAB 7.0?

java - Textview 和 TableRow 权重无法正常工作 Android

android - 如何以编程方式在线性布局中添加 2 个 TextView

android - 如何在 Textview 的字符串中突出显示字符?

Android 音频延迟解决方法

android - 谷歌健身步数

swift - iOS 8 中的 TextView,导航栏位于 TextView 上方

android:每行 TextView 限制为 10 个字符