android中的R.string vs R.id

时间:2014-10-11 16:51:07

标签: android

我有一些代码

MainActivity.java

 import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    public class MainActivity extends Activity {
    TextView av; //UI reference
    int textString = R.string.start;
    int backgroundColor = Color.DKGRAY;
    final Handler mHandler = new Handler();
    // Create runnable for posting results to the UI thread
    final Runnable mUpdateResults = new Runnable() {
    public void run() {
    av.setText(textString);
    av.setBackgroundColor(backgroundColor);
    }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    av = (TextView) findViewById(R.id.computation_status);
    Button actionButton = (Button) findViewById(R.id.action1);
    actionButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
    doWork();
    }
    });
    }
    //example of a computationally intensive action with UI updates
    private void doWork() {
    Thread thread = new Thread(new Runnable() {
    public void run() {
    textString=R.id.start;
    backgroundColor = Color.DKGRAY;
    mHandler.post(mUpdateResults);
    computation(1);
    textString=R.id.first;

    backgroundColor = Color.BLUE;
    mHandler.post(mUpdateResults);
    computation(2);
    textString=R.id.second;
    backgroundColor = Color.GREEN;
    mHandler.post(mUpdateResults);
    }
    });
    thread.start();
    }
    final static int SIZE=1000; //large enough to take some time
    double tmp;
    private void computation(int val) {
    for(int ii=0; ii<SIZE; ii++)
    for(int jj=0; jj<SIZE; jj++)
    tmp=val*Math.log(ii+1)/Math.log1p(jj+1);
    }
    }

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/computation_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000" />

<TextView
    android:id="@+id/start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="start textview" />

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

<TextView
    android:id="@+id/second"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Button
    android:id="@+id/action1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

我知道如果那是R.id.start那么在xml文件中必须有一个名为start的textview。如果我有R.string.start怎么办?因为制作textview不起作用。

1 个答案:

答案 0 :(得分:3)

R.string.start实际上是名为start的字符串的引用,而不是字符串本身。
所以,这是一个int R.id观看次数

的ID