Background changes when the Android control is clicked or selected (button and listview)

Source: Internet
Author: User

User behavior changes, such as user clicks and selections. If you use a button without adding a background image, the button changes when you click it. However, if a background image is added, it does not change when you click it, unless the background image is changed dynamically. User behavior is constantly changing. If every time the background changes are controlled by code, it can be achieved but cannot be controlled. Is there any better way?

Yes.

You can use custom xml as the background image. Take a look.



When you click the button, the background turns yellow. After the button is released, it turns back to the original color. The same is true for items in ListView.

The following describes the specific implementation. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHN0cm9uZz4xLtfUtqjS5bGzvrA8L3N0cm9uZz48L3A + Signature = "brush: java;">

Click_list_item.xml

 
     
      
      
      
  
 
Note:

(1) A selector is defined in. XML. Different items are defined in the selector to respond to different operations. For example, in click_totl.xml, state_pressed indicates the press status, and the corresponding drawable is changed to button_total_click. Normally, it is button_total. The drawble here uses an image. In addition to using images, you can also use colors directly. For example, in click_list_item, yellow and white are used.

(2). color/yellow is a custom color. The Code is as follows.

Clolors. xml

     
  
   #E8E8E8
      
  
   #F88B00
  
 
2. Control Layout

Main_activity.xml

     
      
      
  
 
Note:

(1) The backgroud is set to click_total directly in the button.

(2). listview only defines a control. The specific content must be added to the adapter. The Code is as follows.

DatasAdapter. java

package com.example.cbc;import java.util.ArrayList;import java.util.List;import java.util.UUID;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class DatasAdapter extends BaseAdapter {private List
 
   _datas = null;private Context _context = null;public DatasAdapter(Context context) {_context = context;Init();}private void Init() {_datas = new ArrayList
  
   ();for (int i = 0; i < 20; i++) {_datas.add("[" +i+ "]"+ UUID.randomUUID().toString().substring(0, 10));}}@Overridepublic int getCount() {return _datas.size();}@Overridepublic Object getItem(int position) {return _datas.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {TextView textView =null;if (convertView == null) {textView =new TextView(_context);}else{textView=(TextView)convertView;}textView.setText(_datas.get(position));textView.setTextSize(30);textView.setBackgroundResource(R.drawable.click_list_item);return textView;}}
  
 
Note:

(1) initialize a datas list.

(2) create a new textView in getView and set its background to click_list_item.

(3) determine whether TextView already exists by checking whether convertView is null, so as to avoid wasting resources by creating new views.

3. Implement Activity

MainActivity. java

package com.example.cbc;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;public class MainActivity extends Activity {private ListView _datas_listView=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main_activity);Init();}private void Init() {FetchUIControls();InitParams();}private void FetchUIControls() {_datas_listView=(ListView)findViewById(R.id.list);} private void InitParams() {DatasAdapter datasAdapter=new DatasAdapter(this);_datas_listView.setAdapter(datasAdapter);}}
The button_total and button_total_click images are attached.



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.