Android charsequence

CharSequence類型
這是一個接口,代表的是一個有序字符集合,這個接口包含的方法有:charAt(int index),toString(),length(),subSequence(int start,int end).
這裏需要說的一點就是,對於一個抽象類或者是接口類,不能使用new來進行賦值,但是可以通過以下的方式來進行實例的創建:
CharSequence cs="hello";
但是不能這樣來創建:
CharSequence cs=new CharSequence("hello");
下面來看看一個例子:
TextView tv;    //聲明一個TextView類型的變量tv
CharSequence cs;    //聲明一個CharSequence類型的變量cs
String str;    //聲明一個字符串類型的str變量
cs=getText(R.string.styled_text);    //其實這裏使用的this.getText()方法,即指定上下文的方法
tv=(TextView)findViewById(R.id.styled_text);    //通過給定的id將tv與對應的組件聯繫起來
tv.setText(cs);        //使用這句代碼來設置tv的顯示內容

str=getString(R.string.styled_text);
tv=(TextView)findViewById(R.id.plain_text);
tv.setText(str);

Context context=this;    //這裏使用了Context類型的變量context,指定爲當前上下文
Resources res=context.getResources();        //定義一個Resources類型的變量res,並給它賦值
cs=res.getText(R.string.styled_text);        //獲得R類中指定變量的值
tv=(TextView)findViewById(R.id.styled_text);    //同上
tv.setText(cs);            //設置值

發佈了21 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章