Layout and Control (iv) Response and background of-button

Source: Internet
Author: User

4th Section Button

A button is a control that needs to interact with the user.

ButtonInherit from TextView , whatever is TextView specific, it has.

publicclass Button extends TextView {    ......}
4.1 and TextViewThe different

ButtonA special style is used TextView . In the Button process of creation, it uses a style inside the system,

publicButton(Context context, AttributeSet attrs) {    //创建Button的时候,系统給它使用了特别的style:com.android.internal.R.attr.buttonStyle    this(context, attrs, com.android.internal.R.attr.buttonStyle);}
4.2 ButtonHow to use

ButtonAfter being clicked, you need to respond. Buttonin fact, all View (including TextView ) can add a response.

Android offers two ways to respond.

4.2.1 Setting a response in an XML layout file
  1. To Button add an attribute Android:onclick, let its value be equal to the function name of the listener function. For example, call the onclick

    <Button    ......    <!--设置监听函数onClick()-->    android:onClick="onClick"/>
  2. In Java source code, create a onClick() function, declare public it, return void type, pass in Parameters View ,

    publicvoidonClick(View v){}
  3. Use View getId() , identify the object that needs to be responded to, add a response code,

    publicvoidonClick(View v){    //使用getId(),识别需要响应的对象    switch(v.getId())    {        //添加需要响应的操作代码;        case R.id.button1:        break;    }    break;}
4.2.2 Setting the response in Java source code
  1. In Java source code, create listener,

    onclicklistener Mlistener = new  Onclicklistener () {//this onclick () The function is exactly the same as defined in 4.2.1  public  void  onclick  (View v) {//use GetID () to identify objects that need to be responded to  switch  (V.getid ()) {//Add the action code that needs to be responded to;     } break ; }}
  2. findViewById() Button View The setOnClickListener set-up listener mListener that passes through;

    //获取xml中的Button对象Button btn = (Button) findViewById(R.id.my_button);//设置监听器btn.setOnClickListener( mListener );

Although this is the Button response, in onClick fact, all View can respond to click action.

All controls or layouts are View subclasses, so they are set to listen the same way.

4.3 Button click Effect

In addition to using system-designed button styles, you can define your own buttons to press down and not press down.

  1. On the res\drawable\ directory, right-click to start the wizard to create drawable resource;

  2. Create a selector drawable XML file with the file name button_selector ;

    In this way, res\drawable\ a drawable file named is created in the directory button_selector.xml .

  3. Depending on Button whether they are pressed android:state_pressed , different drawable are set for each of them.

    android:state_pressed=true, indicating that the current button is pressed,
    android:state_pressed=false, indicating that the current button is not pressed;

    Here we use the ColorDrawable demo to set the drawable. The res\values\colors.xml name of the color defined in; Set the color for the android:drawable property,

    <selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="false" android:drawable="@color/colorBtnNormal"/>    <item android:state_pressed="true" android:drawable="@color/colorBtnPressed"/></selector>

    If you use other types of drawable, you are using similar methods.

    For example, the background is a picture: Press use normal.png , not pressed using pressed.png . These pictures are placed under the res\mimap\ directory. The corresponding selector should be written,

    < Selector  xmlns:android  = "HTTP +/ Schemas.android.com/apk/res/android ";  <item  android:state_pressed  = "false"  android:drawable  = "@mipmap/normal" />  Span class= "Hljs-tag" ><item  android:state_ Pressed  = "true"  android:drawable  =< Span class= "Hljs-value" > "@mipmap/pressed" />  </ selector ;   
  4. Give Button the android:background attribute, using drawable selector,

    <Button......android:background="@drawable/button_selector"/>

Layout and Control (iv) Response and background of-button

Related Article

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.