gpt4 book ai didi

android - 如何在任何一个 Activity 中每秒更新标题布局文本并显示在所有屏幕中?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:05:33 24 4
gpt4 key购买 nike

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

谁能建议我只在一个屏幕上设置文本并在所有屏幕上显示?

谢谢

最佳答案

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

按照以下步骤操作。

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

我在这里发布您需要的所有类(class)。

我在 BaseActivity 中做什么?

I create one Countdown Timer class called MyCount which will display the remaining time . I create one boolean flag which set false by default . so when user call setHeading() function of BaseActivity it will check the falg if it false then start the timer and if it is true then again set the timer and starts again .

基础 Activity

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.
}
}
}

主要 Activity

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>

第二个 Activity

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>

关于android - 如何在任何一个 Activity 中每秒更新标题布局文本并显示在所有屏幕中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12634852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com