[Android UI] case 04 configure the control click effect (selector) and androidselector

Source: Internet
Author: User

[Android UI] case 04 configure the control click effect (selector) and androidselector

In this example, XML (selector) is used to configure the implementation of the click effect of the control. That is, when the control is triggered or clicked to get the focus, the style changes, in order to give a good user experience and a sense of operation. This example shows the selector. xml of the core knowledge point to be introduced. For more information, see http://blog.csdn.net/mahoking/article/details/2379857. In this example, TextView and Button controls are used to demonstrate the click effect.

[For more information, see http://blog.csdn.net/mahoking]
First, configure the selector. xml file. Because the test control required in this example contains TextView and Button, we need to configure two XML files in this case, which are located under/res/drawable. Seletor_04_click_textview.xml and seletor_04_click_button.xml.
Seletor_04_click_textview.xml

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


Seletor_04_click_button.xml

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

Seletor_04_click_textview.xml is used to change the font color of TextView. seletor_04_click_button.xml is used to change the background image of the Button. Therefore, we need to prepare two different images for the Button to show the effect of the change.
Then, activity_04_seletor.xml is the main layout file. Note the following:
TextView control configuration (text color)
Android: textColor = "@ drawable/seletor_04_click_textview"
Button Control Configuration (background image)
Android: background = "@ drawable/seletor_04_click_button"

 

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: gravity = "center" android: orientation = "vertical"> <TextView android: layout_height = "wrap_content" android: layout_width = "match_parent" android: layout_marginBottom = "10dp" android: text = "TextView, the text color changes to "android: gravity =" center "/> <TextView android: layout_height =" 30dp "android: layout_width =" match_parent "android: id = "@ + id/activity_04_seletor_textview" android: layout_marginTop = "10dp" android: text = "test TextView" android: textSize = "25dp" android: textColor = "@ drawable/seletor_04_click_textview" android: gravity = "center" android: layout_marginBottom = "40dp"/> <TextView android: layout_height = "wrap_content" android: layout_width = "match_parent" android: layout_marginTop = "10dp" android: text = "The following demo Button changes when you click it." android: textColor = "@ drawable/seletor_04_click_textview" android: layout_marginBottom = "10dp" android: gravity = "center"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/audience" android: gravity = "center" android: background = "@ drawable/seletor_04_click_button" android: layout_marginBottom = "60dp"/> </LinearLayout>

Finally, write the Activity (selecteffectivity) as follows, and then run the project.

Public class selectincluti=extends Activity {private TextView textView; private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_04_seletor); initViews ();}/*** initialize view */private void initViews () {textView = (TextView) findViewById (R. id. activity_04_seletor_textview); button = (Button) findViewById (R. id. activity_04_seletor_button); MainOnClickListener clickListener = new MainOnClickListener (); textView. setOnClickListener (clickListener); button. setOnClickListener (clickListener);} private class MainOnClickListener implements View. onClickListener {@ Overridepublic void onClick (View v) {int viewId = v. getId (); switch (viewId) {case R. id. activity_04_seletor_button: Toast. makeText (getApplicationContext (), "the background image has changed.", Toast. LENGTH_SHORT ). show (); break; case R. id. activity_04_seletor_textview: Toast. makeText (getApplicationContext (), "text color changed", Toast. LENGTH_SHORT ). show (); break; default: break ;}}}}


Effect:


 

 

[For more information, see http://blog.csdn.net/mahoking]



In android ListView, You need to click a certain part of the content. The clicking effect is implemented by selector, but clicking other parts in ListView also works.

TvFileNameAndSize. setText (attachFile. getFileName () + "(" + attachFile. getSize () + "K)");/* Add the file name and size to the display */
V. setTag (attachFile);/* save the file to the control */

ImageView ivDelete = (ImageView) v. findViewById (R. id. iv_iccfa_del );

IvDelete. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View v ){
View parent = (View) v. getParent ();/* Get focus */
AttachFile file = (AttachFile) parent. getTag ();/* export the file imported to this location */
MFiles. remove (file);/* remove it from your collection */
MLlAttachFile. removeView (parent);/* Delete the content corresponding to this focus from the control */
}
});
MLlAttachFile. addView (v );
The format displayed in ListView is a TextView plus an ImageView. This is part of the code that deletes this record when you click the ImageView,

The android ui control Textview must be displayed on the Imageview. The text is under the image.

<FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<ImageView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</FrameLayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.