1、Textview内容设置及显示

 不管什么情况,必须在xml中先声明TextView(可以不设置android:text)

  可以在xml中或者在java中设置TextView文本内容,下面只说如何在java中设置,

    1.1 TextView对象.setText();支持一下多态构造方法

而TextView.setText不支持HTML TAG的输出,比如:text01.setText("<a href=\"http://www.baidu.com\">博客</a>");

不会把这个地址做为链接显示,而是显示<a href=\"http://www.baidu.com\">博客</a>

如果想把一段地址作为链接地址显示,

需要在xml中,TextView的属性设置中,

<TextView 
    android:id="@+id/textView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="all"
    android:text="博客:http://www.baidu.com"
    />

这样就可以了。

   1.2 文本颜色,文本字体大小等

    可以在xml中设置,android:textColor="@drawble/white"

                    android:textSize="18sp"

颜色可以在String.xml中定义 <drawable name="white">#FFFFFFFF</drawable>

                         <drawable name="darkgray">#808080FF</drawable>

    也可以在java中设置,

如:text01.setTextColor(R.drawable.darkgray);或者mTextView02.setTextColor(Color.BLUE);均可,

    text01.setTextSize(20);

   1.3 设置EditText输入内容为密码

   在xml中设置EditText 的属性,anroid:password=true;

   1.4 设置TextView字体的背景颜色

 Resources resources=getBaseContext().getResources();

 Drawable drawable =resources.getDrawble(R.drawable.white);

text01.setBackgroundDrawable(drawable);

  1.5 获取在string.xml中定义的字符串

如下:

mTextView01,mTextView02都是在xml中已经定义了内容的,

        mTextView01 = (TextView) findViewById(R.id.myTextView01);
        mTextView02 = (TextView) findViewById(R.id.myTextView02);

关键是以下这两句:
        CharSequence char1=getString(R.string.str_textview01);
        CharSequence char2=getString(R.string.str_textview02);
        String s="haha";
        mTextView02.setText(char1+s+char2);

在setText();中,不能char1+char2,这跟setText的构造方法有关。

1.6 连续不断设置TextView字体颜色

java代码

代码
package irdc.Ex03_13;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Ex03_13 extends Activity
{
private Button mButton;
private TextView mText;
private int[] mColors;
private int colornum;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton
= (Button) findViewById(R.id.mybutton);
mText
= (TextView) findViewById(R.id.mytext);
/* mColors = new int[]
{ Color.BLACK, Color.RED, Color.BLUE, Color.GREEN, Color.MAGENTA,
Color.YELLOW };
*/
mColors
=new int[]{R.drawable.black,R.drawable.blue,R.drawable.cyan,R.drawable.darkgray,R.drawable.gray,R.drawable.green,R.drawable.lightgray,R.drawable.magenta,R.drawable.red,R.drawable.white,R.drawable.yellow};
colornum
= 0;
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View v)
{
if (colornum < mColors.length)
{
//add by surx start
// int color=getResources().getColor(mColors[colornum]);
Resources resources=getBaseContext().getResources();
int color= resources.getColor(mColors[colornum]);
//surx end
//mText.setTextColor(mColors[colornum]);
mText.setTextColor(color);
colornum
++;
}
else
colornum
= 0;
}
});
}
}

color.xml

代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="darkgray">#ff444444</drawable>
<drawable name="black">#ff000000</drawable>
<drawable name="red">#ffff0000</drawable>
<drawable name="green">#ff00ff00</drawable>
<drawable name="lightgray">#ffcccccc</drawable>
<drawable name="white">#ffffffff</drawable>
<drawable name="yellow">#ffffff00</drawable>
<drawable name="blue">#ff0000ff</drawable>
<drawable name="gray">#ff888888</drawable>
<drawable name="magenta">#ffff00ff</drawable>
<drawable name="cyan">#ff00ffff</drawable>
</resources>

string.xml

代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Ex03_13</string>
<string name="app_name">Ex03_13</string>
<string name="textview_str">看我,变颜色啦!</string>
<string name="button_str">按我,让字体变颜色</string>
</resources>

main.xml

代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget27"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:background
="@drawable/white"
xmlns:android
="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
>
<TextView
android:id="@+id/mytext"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/textview_str">
</TextView>
<Button
android:id="@+id/mybutton"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/button_str"
>
</Button>
</LinearLayout>



注意:从resources资源里面可以获取很多东西,比如getDrawable,getColor,上面的setTextColor需要的是int 类型的数据,所以不能用resources.getDrawable(),这样得到的是一个Drawable类型的变量。resources.getColor()得到的是int类型的变量,用来表示颜色。

posted on 2010-11-27 11:32  snowdrop  阅读(624)  评论(0编辑  收藏  举报