改变 Android“正在加载..”文本

发布于 2024-12-12 09:37:21 字数 3570 浏览 0 评论 0原文

所以这可能是我正在尝试做的一件简单的事情,但我正在尝试显示默认的 android 加载屏幕,并让文本改为“正在消耗...”我正在根据教程进行此操作Android 食谱。这是我的代码和 xml 文件。如果您需要任何其他代码,请告诉我。老实说,我可能正在做一些愚蠢的事情,但我只需要知道。 logcat 中没有发生任何事情,所以我确定问题是什么。不过,这本书可能已经过时了。我还应该指出,我正在运行 Android 2.2,并且正在 Samsung Galaxy 4G 上进行测试。

所以我想需要更多信息。我正在尝试遵循 Android 开发人员食谱中的处理耗时的初始化配方: http://bit.ly/sFoBn6 我只想要一个简单的文本启动屏幕。连进度条都没有。我们现在谈论的是简单的愚蠢。我还在学习Android。我只想让它执行正常的“正在加载..”,但让它说“正在消耗...”

HandleMessage.java

package com.cookbook.handle_message;

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

public class HandleMessage extends Activity implements Runnable {
    /** Called when the activity is first created. */
    TextView mTestLabel;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTestLabel = (TextView) findViewById(R.id.test);

        Thread thread = new Thread(this);
        thread.start();
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            setContentView(R.layout.main);
        }
    };

    public void run() {
        initializeArrays();
        mHandler.sendEmptyMessage(0);
    }

    final static int NUM_SAMPS = 1000;
    static double[][] correlation;

    void initializeArrays() {
        if(correlation!=null) return;

        correlation = new double[NUM_SAMPS][NUM_SAMPS];
        for(int k=0; k<NUM_SAMPS; k++){
            for(int m=0; m<NUM_SAMPS; m++){
                correlation[k][m] = Math.cos(2*Math.PI*(k+m)/1000);
            }
        }
    }
}

res/layout/main.xml

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


    <TextView
        android:id="@+id/test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

res/layout/loading.xml

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

    <TextView android:id="@+id/loading"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Consuming..."/>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cookbook.handle_message"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".HandleMessage" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

So this is probably an easy thing that I'm trying to do but I'm trying to get the default android loading screen to show and have the text instead say "Consuming..." I'm doing this based off the tutorial in the Android Cookbook. Here are my code and xml files. If you need any other bits of code, just let me know. I honestly am probably doing something stupid but I just need to know. There is nothing happening in logcat so I'm sure what the issue is. Though, the book may just be outdated. I should also note that I'm running of Android 2.2 and I'm testing on a Samsung Galaxy 4G.

So I guess a bit more info is needed. I'm trying to follow the Handling a Time-Consuming Initialization recipe in the Android Developer's Cookbook: http://bit.ly/sFoBn6
I just want a simple text splash screen. Not even a progress bar. We're talking simple stupid right now. I'm still learning Android. I just want it to do the normal "Loading.." but instead have it say "Consuming..."

HandleMessage.java

package com.cookbook.handle_message;

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

public class HandleMessage extends Activity implements Runnable {
    /** Called when the activity is first created. */
    TextView mTestLabel;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTestLabel = (TextView) findViewById(R.id.test);

        Thread thread = new Thread(this);
        thread.start();
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            setContentView(R.layout.main);
        }
    };

    public void run() {
        initializeArrays();
        mHandler.sendEmptyMessage(0);
    }

    final static int NUM_SAMPS = 1000;
    static double[][] correlation;

    void initializeArrays() {
        if(correlation!=null) return;

        correlation = new double[NUM_SAMPS][NUM_SAMPS];
        for(int k=0; k<NUM_SAMPS; k++){
            for(int m=0; m<NUM_SAMPS; m++){
                correlation[k][m] = Math.cos(2*Math.PI*(k+m)/1000);
            }
        }
    }
}

res/layout/main.xml

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


    <TextView
        android:id="@+id/test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

res/layout/loading.xml

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

    <TextView android:id="@+id/loading"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Consuming..."/>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cookbook.handle_message"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".HandleMessage" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2024-12-19 09:37:21

也许您只是想要一个进度指示器或其他东西?请参阅此页面的示例,该示例展示了如何使用长时间运行的线程,随着进度条的进行而更新进度条。

另外,您表示 LogCat 没有说什么。在代码中添加一些 Log.i 语句来查看发生了什么。

编辑:好的,在上面的代码中,不要使用 2 种布局。仅使用 1。然后在处理程序中更改 textview 的文本。

因此,如果您使用 loading.xml,请在 onCreate 中设置它,然后在处理程序中执行以下操作:

 public void handleMessage(Message msg) {
        TextView tv=(TextView)findViewById(R.id.loading);
        tv.setText("All Done");
    }

得到它吗?绝对不要为这种事情调用两次 setContentView 。可能有办法做到这一点,所以也许它有它的用处,但我从未这样做过。另外,作为旁注,我开始使用 AsyncTask 而不是线程和处理程序来处理某些内容。

另外,每当出现问题时,请添加 Log 语句,以便您可以了解发生了什么情况。

Maybe you just want a progress indicator or something? See this page for an example that shows how to start an activity with a long running thread that updates the progress bar as it goes along.

Also, you indicated LogCat says nothing. Put some Log.i statements in your code to see what's going on.

EDIT: Ok, in your code above, don't use 2 layouts. Use just 1. Then in your handler change the text of the textview.

So if you use loading.xml, set that in onCreate and then in your handler just do something like:

 public void handleMessage(Message msg) {
        TextView tv=(TextView)findViewById(R.id.loading);
        tv.setText("All Done");
    }

Get it? Definitely don't call setContentView twice for this kind of thing. There may be a way to do that, so maybe it has its uses, but I've never done it. Also, as a side note, I've started using AsyncTask instead of threads and Handlers for some stuff.

Also, whenever something isn't working, please add Log statements so that you can tell what's going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文