码迷,mamicode.com
首页 > 移动开发 > 详细

android基础组件---->Buttons的使用

时间:2016-05-05 08:25:32      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

  按钮由文本或图标(或文本和一个图标)组成,当用户触摸到它时,会发生一些动作。今天我们开始Button的学习。

Button的简要说明

根据你是否想要一个带有文本的按钮,一个图标,或者两者,你可以在三种方式中创建按钮来进行布局:

  技术分享

  • With text, using the Button class:
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    ... />
  • With an icon, using the ImageButton class:
<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/button_icon"
    ... />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:drawableLeft="@drawable/button_icon"
    ... />

 

按钮的响应

1) xml中定义的Button中增加属性android:onClick

  • 在xml中定义android:onClick属性:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />
  • 在MainActivity中定义sendMessage方法:
public void sendMessage(View view) {
    // Do something in response to button click
}

2) 在代码中使用OnClickListener

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do something in response to button click
    }
});

 

按钮样式

1) Borderless button : To create a borderless button, apply the borderlessButtonStyle style to the button.

<Button
    android:onClick="sendMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bordless button"
    style="?android:attr/borderlessButtonStyle" />

2) Custom background: Instead of supplying a simple bitmap or color, however, your background should be a state list resource that changes appearance depending on the button‘s current state

  • Create three bitmaps for the button background that represent the default, pressed, and focused button states. 
  • Place the bitmaps into the res/drawable/ directory of your project. Be sure each bitmap is named properly to reflect the button state that they each represent.

  技术分享

  • Create a new XML file in the res/drawable/ directory (name it something like button_custom.xml). Insert the following XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
        android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused"
        android:state_focused="true" />
    <item android:drawable="@drawable/button_default" />
</selector>
  • Then simply apply the drawable XML file as the button background
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="send button"
    android:background="@drawable/button_custom"  />

 

测试的项目

 项目结构:

技术分享

MainActivity.java:

package com.huhx.linux.buttontest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.sendButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "button click", Toast.LENGTH_SHORT).show();
            }
        });
    }

    // bordless button
    public void sendMessage(View view) {
        Toast.makeText(MainActivity.this, "Borderless Button", Toast.LENGTH_SHORT).show();
    }
}

 button_custom.xml

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

 activity_main.xml.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.huhx.linux.buttontest.MainActivity">

    <Button
        android:id="@+id/sendButton"
        android:text="text button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageButton
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:drawableRight="@mipmap/ic_launcher"
        android:text="Linux"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:onClick="sendMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bordless button"
        style="?android:attr/borderlessButtonStyle" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send button"
        android:background="@drawable/button_custom"  />
</LinearLayout>

 运行效果如下:

技术分享

android基础组件---->Buttons的使用

标签:

原文地址:http://www.cnblogs.com/huhx/p/base_button.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!