【Android UI】案例04配置控件点击效果(selector)

前端之家收集整理的这篇文章主要介绍了【Android UI】案例04配置控件点击效果(selector)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本例采用XML(selector),配置控件点击效果的实现,即当控件被触发或点击获取到焦点时,出现样式上的改变,以便给以较好的用户体验与操作感。本例需要引入的核心知识点的selector.xml。请参考学习:http://www.jb51.cc/article/p-kxglgcyc-uy.html。本例用于演示点击效果的控件为TextView、Button。

【转载使用,请注明出处:http://blog.csdn.net/mahoking
首先需要配置selector.xml文件,因为本例需要的测试控件包含TextView和Button,所以本案例需要配置两个XML文件文件位于/res/drawable下。分别为:seletor_04_click_textview.xml、seletor_04_click_button.xml。
seletor_04_click_textview.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_pressed="true" android:color="#18A960" />
  4. <item android:state_focused="true" android:color="#18A960" />
  5. <item android:state_selected="true" android:color="#18A960" />
  6. <item android:color="@android:color/black"></item>
  7. </selector>


seletor_04_click_button.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_pressed="true" android:drawable="@drawable/button_pressed" />
  4. <item android:state_focused="true" android:drawable="@drawable/button_pressed" />
  5. <item android:state_selected="true" android:drawable="@drawable/button_pressed" />
  6. <item android:drawable="@drawable/button_default"></item>
  7. </selector>

seletor_04_click_textview.xml旨在改变TextView的字体颜色,seletor_04_click_button.xml旨在改变Button的背景图片,所以我们需要给Button准备两张不同的图片,以显示改变的效果
然后是主布局文件activity_04_seletor.xml。需要特别注意的地方是:
TextView控件的配置(文本颜色)
android:textColor="@drawable/seletor_04_click_textview"
Button控件的配置(背景图片)
android:background="@drawable/seletor_04_click_button"

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7. <TextView android:layout_height="wrap_content"
  8. android:layout_width="match_parent"
  9. android:layout_marginBottom="10dp"
  10. android:text="以下演示TextView,点击时文字颜色改变"
  11. android:gravity="center"/>
  12. <TextView android:layout_height="30dp"
  13. android:layout_width="match_parent"
  14. android:id="@+id/activity_04_seletor_textview"
  15. android:layout_marginTop="10dp"
  16. android:text="测试使用TextView"
  17. android:textSize="25dp"
  18. android:textColor="@drawable/seletor_04_click_textview"
  19. android:gravity="center"
  20. android:layout_marginBottom="40dp"/>
  21. <TextView android:layout_height="wrap_content"
  22. android:layout_width="match_parent"
  23. android:layout_marginTop="10dp"
  24. android:text="以下演示Button,点击时背景图片改变"
  25. android:textColor="@drawable/seletor_04_click_textview"
  26. android:layout_marginBottom="10dp"
  27. android:gravity="center"/>
  28. <Button android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:id="@+id/activity_04_seletor_button"
  31. android:gravity="center"
  32. android:background="@drawable/seletor_04_click_button"
  33. android:layout_marginBottom="60dp"/>
  34. </LinearLayout>

最后编写Activity(SelectorActivity)如下,完成后运行该项目。

  1. public class SelectorActivity extends Activity {
  2.  
  3. private TextView textView;
  4. private Button button;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_04_seletor);
  9. initViews();
  10. }
  11. /**
  12. * 初始化视图
  13. */
  14. private void initViews() {
  15.  
  16. textView = (TextView) findViewById(R.id.activity_04_seletor_textview);
  17. button = (Button) findViewById(R.id.activity_04_seletor_button);
  18. MainOnClickListener clickListener = new MainOnClickListener();
  19. textView.setOnClickListener(clickListener);
  20. button.setOnClickListener(clickListener);
  21. }
  22. private class MainOnClickListener implements View.OnClickListener{
  23.  
  24. @Override
  25. public void onClick(View v) {
  26. int viewId = v.getId();
  27. switch (viewId) {
  28. case R.id.activity_04_seletor_button:
  29. Toast.makeText(getApplicationContext(),"背景图片发生改变",Toast.LENGTH_SHORT).show();
  30. break;
  31. case R.id.activity_04_seletor_textview:
  32. Toast.makeText(getApplicationContext(),"文字颜色发生改变",Toast.LENGTH_SHORT).show();
  33. break;
  34. default:
  35. break;
  36. }
  37. }
  38. }
  39. }


效果截图:


【转载使用,请注明出处:http://blog.csdn.net/mahoking

猜你在找的XML相关文章