如何在android中使用textview显示正在使用的功能?

时间:2016-01-26 10:37:22

标签: android

我希望有一个布局显示当前使用的功能消息。

在运行下面的代码时,它只显示最新的TextView,而不是所有TextView使用的

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        LinearLayout linear = new LinearLayout(this);
        TextView tv=new TextView(this);
        tv.setText("Created");
        linear.addView(tv);
        setContentView(linear);
    }

    @Override
    protected void onStart()
    {
        super.onStart();

        LinearLayout linear = new LinearLayout(this);
        TextView tv=new TextView(this);
        tv.setText("Started");
        linear.addView(tv);
        setContentView(linear);    }


    @Override
    protected void onResume()
    {
        super.onResume();
        LinearLayout linear = new LinearLayout(this);
        TextView tv=new TextView(this);
        tv.setText("Resumed");
        linear.addView(tv);
        setContentView(linear);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

}

2 个答案:

答案 0 :(得分:0)

您在每个功能中都使用setContentViewsetContentView替换Activity之前定义的任何视图。您想在每个函数上添加TextView文本。因此,您应该在onCreate中设置内容视图,然后获取视图,并在TextView中添加的LinearLayout中添加onCreate。创建全局线性布局,然后使用它,如下所示

LinearLayout linear;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
linear = new LinearLayout(this);
    TextView tv=new TextView(this);
    tv.setText("Created");
    linear.addView(tv);
    setContentView(linear);
}

@Override
protected void onStart()
{
    super.onStart();

    TextView tv=new TextView(this);
    tv.setText("Started");
    linear.addView(tv);
 }


@Override
protected void onResume()
{
    super.onResume();
    TextView tv=new TextView(this);
    tv.setText("Resumed");
    linear.addView(tv);
}

希望这有帮助。

答案 1 :(得分:0)

活动类是,

public class MainActivity extends AppCompatActivity {


TextView tvStart, tvCreate, tvResume;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvCreate = (TextView)findViewById(R.id.tvCreate);
    tvStart = (TextView)findViewById(R.id.tvStart);
    tvResume = (TextView)findViewById(R.id.tvResume);



    tvCreate.setText("Created");

}


@Override
protected void onStart() {
    super.onStart();Started

    tvStart.setText("Started");
}


@Override
protected void onPostResume() {
    super.onPostResume();

    tvResume.setText("Resumed");
}
}

xml for this,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.technobd.stack.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">

<TextView
    android:id="@+id/tvCreate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

<TextView
    android:id="@+id/tvStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

<TextView
    android:id="@+id/tvResume"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

您还可以动态添加文字视图,例如, 活动类将是,

public class MainActivity extends AppCompatActivity {


LinearLayout llTextViews;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    llTextViews = (LinearLayout)findViewById(R.id.llTextViews);


    TextView tvCreate = new TextView(MainActivity.this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    tvCreate.setLayoutParams(params);
    tvCreate.setText("Created");

    llTextViews.addView(tvCreate);

}


@Override
protected void onStart() {
    super.onStart();

    TextView tvStart = new TextView(MainActivity.this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    tvStart.setLayoutParams(params);
    tvStart.setText("Started");

    llTextViews.addView(tvStart);

}


@Override
protected void onPostResume() {
    super.onPostResume();

    TextView tvResume = new TextView(MainActivity.this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    tvResume.setLayoutParams(params);
    tvResume.setText("Resumed");

    llTextViews.addView(tvResume);
}
}

和xml布局是,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.technobd.stack.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/llTextViews"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"></LinearLayout>
</LinearLayout>