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

时间:2011-10-29 04:16:50

标签: android android-layout

所以这可能是我想要做的一件容易的事情,但我正试图让默认的Android加载屏幕显示并让文字代替说“消费......”我这样做是基于Android Cookbook中的教程。这是我的代码和xml文件。如果您需要任何其他代码,请告诉我。老实说,我可能做了一些愚蠢的事情,但我只是需要知道。在logcat中没有任何事情发生,所以我确定问题是什么。虽然,这本书可能只是过时了。我还应该注意到我正在运行Android 2.2,而我正在测试三星Galaxy 4G。

所以我想需要更多信息。我正在尝试遵循Android Developer's Cookbook中处理耗时的初始化配方: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 /布局/ 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 /布局/ 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>

1 个答案:

答案 0 :(得分:0)

也许你只想要一个进度指示器或什么?请参阅this page for an example,其中显示了如何使用长时间运行的线程启动活动,该线程会随着时间的推移更新进度条。

另外,你表示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 而不是线程和处理程序来处理某些事情。

此外,每当某些内容无效时,请添加日志语句,以便了解发生了什么。