Click button through code

Source: Internet
Author: User

1. The effects of custom states can be implemented through code or styles defined in XML.

2. The following describes the code implementation and uses statelistdrawable to define the button background.

3. Because the pressed_enabled_state_set value in the View class is not a public constant, it is accessed through inheritance.

Note: The effects of other controls, such as imageview, can also be achieved through this method. However, because imageview does not have focus by default and cannot be clicked, you need to change it by yourself (you need to click to set Android: clickable = "true", you need to be able to select and set Android: focusable = "true ").

Java code:

01 package COM. test. testbutton; 02 03 import android. app. activity; 04 import android. content. context; 05 import android. graphics. drawable. drawable; 06 import android. graphics. drawable. statelistdrawable; 07 import android. OS. bundle; 08 import android. view. view; 09 import android. widget. button; 10 11 public class testbutton extends activity {12 @ override13 public void oncreate (bundle savedinstancestate) {14 super. onc Reate (savedinstancestate); 15 setcontentview (R. layout. main); 16 integer [] mbuttonstate = {R. drawable. defaultbutton, 17 R. drawable. focusedpressed, R. drawable. pressed}; 18 button mbutton = (button) findviewbyid (R. id. button); 19 mybutton = new mybutton (this); 20 mbutton. setbackgrounddrawable (mybutton. setbg (mbuttonstate); 21} 22 23 class mybutton extends view {24 25 public mybutton (context contex T) {26 super (context); 27} 28 29 // The following method can also pass your image array and set the image status with statelistdrawable, to display the states of the buttons. 30 // not selected, press to select the effect. 31 public statelistdrawable setbg (integer [] mimageids) {32 statelistdrawable BG = new statelistdrawable (); 33 drawable normal = This. getresources (). getdrawable (mimageids [0]); 34 drawable selected = This. getresources (). getdrawable (mimageids [1]); 35 drawable pressed = This. getresources (). getdrawable (mimageids [2]); 36 BG. addstate (view. pressed_enabled_state_set, pressed); 37 BG. addstate (view. enabled_focused_state_set, selected); 38 BG. addstate (view. enabled_state_set, normal); 39 BG. addstate (view. focused_state_set, selected); 40 BG. addstate (view. empty_state_set, normal); 41 return BG; 42} 43} 44}

Main. xml

01<?xml version=”1.0″ encoding=”utf-8″?>02<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”03        android:orientation=”vertical”04        android:layout_width=”fill_parent”05        android:layout_height=”fill_parent”06        >07    <Button android:id=”@+id/btn”08            android:layout_width=”wrap_content”09            android:layout_height=”wrap_content”10            android:text=”@string/mybtn”11            android:background=”@drawable/mybutton_background” />12</LinearLayout>

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.