4

header.xml

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/txtTime"               
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:gravity="right" />
    </LinearLayout>

我添加header.xml了我所有的屏幕布局......

 <include
        android:id="@+id/top_header"
        layout="@layout/header" />

现在我想timer在文本标题中开始并设置剩余秒数的文本txtTime......所以我为倒数计时器编写代码并将textview所有屏幕显示时间设置为textview..

有人可以建议我只设置一个屏幕的文本并在所有屏幕上显示吗?

谢谢

4

2 回答 2

2

请尝试以下代码。它将解决您的问题。

请按照以下步骤操作。

1) Make one BaseActivity Class.
2) Make One MainActivity Class and extend BaseActivity.
3) Make another SecondActivity class and extend BaseActivity.

在这里,我发布了您需要的所有课程。

我在 BaseActivity 做什么?

我创建了一个名为 MyCount 的 Countdown Timer 类,它将显示剩余时间。我创建了一个默认设置为 false 的布尔标志。因此,当用户调用 BaseActivity 的 setHeading() 函数时,它将检查 falg 是否为假,然后启动计时器,如果为真,则再次设置计时器并再次启动。

基本活动

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

public abstract class BaseActivity extends Activity
{
    private TextView textView = null;
    public static MyCount counter = null; 
    private static boolean flag = false;
    private static long millisUntilFinishedVariable = 0;

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

    }

    protected void setHeading(String message) 
    {
        if(textView == null)
        {
            textView = (TextView)findViewById(R.id.textView);
            if(textView != null)
                textView.setText(message);

            if(flag == false)
            {
                counter = new MyCount(30000, 1000);
                counter.start();
            }
            else
            {
                            counter.cancel();
                counter = new MyCount(millisUntilFinishedVariable, 1000);
                counter.start();
            }
        }
    }

    // countdowntimer is an abstract class, so extend it and fill in methods
    public class MyCount extends CountDownTimer
    {

        public MyCount(long millisInFuture, long countDownInterval) 
        {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() 
        {
            textView.setText("done!");   //TextView object should be defined in onCreate
            flag = false;
        }

        @Override
        public void onTick(long millisUntilFinished) 
        {
            millisUntilFinishedVariable = millisUntilFinished;
            flag = true;
            textView.setText("Left:" + millisUntilFinished/1000);// This will be called every Second.
        }
    }
} 

主要活动

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends BaseActivity 
{
    private Button buttonClick = null;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setHeading("Text From Main Activity"); 

        buttonClick = (Button)findViewById(R.id.buttonClick);
        buttonClick.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View arg0) 
            {
                startActivityForResult(new Intent(MainActivity.this,SecondActivity.class), 0);
            }
        });
    }
}

activity_main.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" >

   <include layout="@layout/header"/>

   <Button
       android:id="@+id/buttonClick"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:layout_marginLeft="96dp"
       android:layout_marginTop="68dp"
       android:text="Second Activity" />

</RelativeLayout>

第二活动

import android.os.Bundle;

public class SecondActivity extends BaseActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        setHeading("From Second Activity");
    }
}

second_activity.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" >

   <include layout="@layout/header"/>

</RelativeLayout>

header.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/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18.0sp"/>

</LinearLayout>
于 2012-09-28T08:52:52.153 回答
1

您可以使用SharedPreferences保存要设置到所有屏幕的文本。然后textViews使用保存的文本设置 。您可以在textView所有活动中设置。onCreate()onResume()

于 2012-09-28T06:58:18.673 回答