TextView tvItem1 = (TextView) findViewById (R.id.tvItem1);
TextView tvItem2 = (TextView) findViewById (R.id.tvItem2);
TextView tvItem3 = (TextView) findViewById (R.id.tvItem3);
TextView tv_id[] = {tvItem1, tvItem2, tvItem3};
public void setBackgroundFocus(int mPosition)
{
int position = mPosition-1;
for(int i=0; i<tv_id.length; i++)
{
if(i == position)
{
//若掃到的項目為要focus的項目, 則改變被景色
tv_id[i].setBackgroundResource(R.drawable.focused);
}
else
{
//其它項目則不是focused的背景色
tv_id[i].setBackgroundResource(R.drawable.unfocus);
}
}
}
tvItem1.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
mPosition = 1;
//因進入focus與離開focus都會呼叫onFocusChange(), 故需判定目前hasFocus是否為true
if(hasFocus == true)
{
setCategoryBackground_focus(mPosition);
}
else
{
}
}
});
tvItem2.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
mPosition = 2;
//因進入focus與離開focus都會呼叫onFocusChange(), 故需判定目前hasFocus是否為true
if(hasFocus == true)
{
setCategoryBackground_focus(mPosition);
}
else
{
}
}
});
tvItem3.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
mPosition = 3;
//因進入focus與離開focus都會呼叫onFocusChange(), 故需判定目前hasFocus是否為true
if(hasFocus == true)
{
setCategoryBackground_focus(mPosition);
}
else
{
}
}
});
總網頁瀏覽量
基礎Note
☪About Me
(1)
免費軟體
(2)
教學
(4)
教學文件
(42)
會計軟體
(1)
電腦系統
(1)
Adapter
(8)
Adobe Premiere
(1)
AlertDialog
(7)
Android App 介紹
(1)
Animation
(1)
API
(2)
APP範例
(1)
Array
(1)
AsyncTask
(1)
Auto Test Case
(32)
AutoCompleteTextView
(1)
Bitmap Drawable
(3)
BroadcastReceiver
(4)
Button
(1)
Codility
(2)
Contact
(4)
DB
(1)
Dialog
(2)
Documents
(1)
Eclipse
(3)
Ellipsize
(1)
File
(4)
Focus
(2)
Fragment
(4)
Gallery
(2)
GIT
(4)
GitHub
(1)
GridView
(8)
HashMap
(1)
HorizontalScrollView
(6)
IIS
(1)
Intent
(3)
IntentService
(1)
Internet
(2)
KeyEvent
(1)
Layout
(1)
ListView
(11)
Log
(1)
Mac / iOS
(11)
Manifest
(1)
Marquee
(2)
Math
(1)
MediaPlayer
(5)
MediaRecorder
(5)
MSMQ
(1)
onClick
(1)
PackageManager
(6)
PHP
(1)
PIS
(3)
PowerManager
(1)
Progress
(2)
SCREEN
(1)
Search
(6)
Service
(1)
SharedPreferences
(3)
SimpleDateFormat
(1)
SonarQube
(1)
Sound Recorder
(1)
Spinner
(2)
SQL server Management
(16)
SQLite
(13)
String
(1)
STS
(5)
SVN
(1)
Thread
(1)
Toast
(3)
Typeface
(1)
Uri
(2)
VB.NET
(17)
VMware
(1)
關於我自己
2012年3月11日 星期日
2012年2月21日 星期二
Focus/Select/Pressed vs 換圖/改變字體顏色
在此以TextView與GridView為例:
TextView必須設定focus屬性, 才能使focused/pressed行為生效, 即這兩行
接下來需要在./res/drawable/自訂.xml來規範focused/pressed行為
state_focused="true"時, 字體顏色為黑色
state_pressed="true"時, 字體顏色為紅色
<item
android:color="@color/white"
/>
表示既沒有focused也沒有pressed時, 字體顏色為白色
./res/drawable/selector_communication_textcolor.xml
state_focused="true"時, 換一張背景圖label_focus.png
state_pressed="true"時, 換一張背景圖label_pressed.png
既沒有focused也沒有pressed時, 表示沒有圖
./res/drawable/selector_communication_background.xml
在GridView中不必設
android:focusable="true"
android:focusableInTouchMode="true"
因為它有專屬的屬性可以建立這種功能, 即android:listSelector
android:verticalSpacing="40dip"←表示每一列資料的垂直間隔是40dip
android:numColumns="6" ←表示每一列可以放6筆資料
android:fadingEdge="none" ←當資料超過頁面需要拖拉時, 不要產生陰影的特殊效果
一樣在./res/drawable/自訂.xml來規範focused/pressed行為
state_focused="true"時, 換一張圖item_focus.png
state_pressed="true"時, 換一張圖item_pressed.png
既沒有focused也沒有pressed時, 表示沒有圖
./res/drawable/selector_list_background.xml
但在變換TextView文字的部份, 有一種作法是再寫一個layout來定義GridView的資料內容
gridview_item.xml的引用會在source code裡
值得注意的是, 在此得用selected/pressed
state_selected="true"時, 字體顏色為黑色
state_pressed="true"時, 字體顏色為紅色
<item
android:color="@color/white"
/>
表示既沒有selected也沒有pressed時, 字體顏色為白色
./res/drawable/selector_list_textcolor.xml
android:color="@color/black"
android:color="@color/red"
android:color="@color/white"
則是預先在./res/values/colors.xml中定義好
<resources>
<color name="black">#000000</color>
<color name="red">#FF0000</color>
<color name="white">#FFFFFF</color>
</resources>
TextView必須設定focus屬性, 才能使focused/pressed行為生效, 即這兩行
android:focusable="true"
android:focusableInTouchMode="true"
android:focusableInTouchMode="true"
<TextView
android:id="@+id/tv_communication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Communication"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColor="@drawable/selector_communication_textcolor"
android:background="@drawable/selector_communication_background"
/>
接下來需要在./res/drawable/自訂.xml來規範focused/pressed行為
state_focused="true"時, 字體顏色為黑色
state_pressed="true"時, 字體顏色為紅色
<item
android:color="@color/white"
/>
表示既沒有focused也沒有pressed時, 字體顏色為白色
./res/drawable/selector_communication_textcolor.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:color="@color/black"
/>
<item
android:state_focused="true"
android:state_pressed="true"
android:color="@color/red"
/>
<item
android:state_focused="false"
android:state_pressed="true"
android:color="@color/red"
/>
<item
android:color="@color/white"
/>
</selector>
state_focused="true"時, 換一張背景圖label_focus.png
state_pressed="true"時, 換一張背景圖label_pressed.png
既沒有focused也沒有pressed時, 表示沒有圖
./res/drawable/selector_communication_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/label_focus"
/>
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/label_pressed"
/>
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/label_pressed"
/>
</selector>
在GridView中不必設
android:focusable="true"
android:focusableInTouchMode="true"
因為它有專屬的屬性可以建立這種功能, 即android:listSelector
android:verticalSpacing="40dip"←表示每一列資料的垂直間隔是40dip
android:numColumns="6" ←表示每一列可以放6筆資料
android:fadingEdge="none" ←當資料超過頁面需要拖拉時, 不要產生陰影的特殊效果
<GridView
android:id="@+id/gv_list"
android:listSelector="@drawable/selector_list_background"
android:verticalSpacing="40dip"
android:numColumns="6"
android:fadingEdge="none"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
一樣在./res/drawable/自訂.xml來規範focused/pressed行為
state_focused="true"時, 換一張圖item_focus.png
state_pressed="true"時, 換一張圖item_pressed.png
既沒有focused也沒有pressed時, 表示沒有圖
./res/drawable/selector_list_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/item_focus"
/>
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/item_pressed"
/>
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/item_pressed"
/>
</selector>
但在變換TextView文字的部份, 有一種作法是再寫一個layout來定義GridView的資料內容
./res/layout/gridview_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/name"
android:layout_width="280dp"
android:layout_height="280dp"
android:textColor="@drawable/selector_list_textcolor"
/>
</LinearLayout>
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/name"
android:layout_width="280dp"
android:layout_height="280dp"
android:textColor="@drawable/selector_list_textcolor"
/>
</LinearLayout>
gridview_item.xml的引用會在source code裡
LayoutInflater mInflater;
public View getView(int position, View convertView, final ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.gridview_item, null);
}
.
.
.
}
public View getView(int position, View convertView, final ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.gridview_item, null);
}
.
.
.
}
值得注意的是, 在此得用selected/pressed
state_selected="true"時, 字體顏色為黑色
state_pressed="true"時, 字體顏色為紅色
<item
android:color="@color/white"
/>
表示既沒有selected也沒有pressed時, 字體顏色為白色
./res/drawable/selector_list_textcolor.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:state_pressed="false"
android:color="@color/black"
/>
<item
android:state_selected="true"
android:state_pressed="true"
android:color="@color/red"
/>
<item
android:state_selected="false"
android:state_pressed="true"
android:color="@color/red"
/>
<item
android:color="@color/white"
/>
</selector>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
至於前面提到的android:color="@color/black"
android:color="@color/red"
android:color="@color/white"
則是預先在./res/values/colors.xml中定義好
<resources>
<color name="black">#000000</color>
<color name="red">#FF0000</color>
<color name="white">#FFFFFF</color>
</resources>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
訂閱:
文章 (Atom)