来个Button看一看

时间:2023-03-10 05:22:11
来个Button看一看

0.目录

1.前言

2.基本属性与方法

3.点点更健康

4.我的Button有点多

5.震惊!TextView竟然...

1.前言

每次写代码总会忘记一些东西,又要重新Goooooooooogle,好烦呐~

本文参考网站(排名不分先后):

1.Android Button的基本使用

2.Android中设置文本颜色的三种方法

3.android:layout_gravity和android:gravity的区别

2.基本属性与方法

Button 支持的 XML 属性及相关方法:

XML属性 相关方法 说明
android:id findViewById 在XML中设置id,然后在.java中才可以调用这个按钮做其他事
android:text setText() 设置文字
android:textColor setTextColor() 设置文字颜色
android:textSize setTextSize() 设置文字大小
android:background setBackground() 设置背景颜色或者背景图片
android:enabled setEnabled() 设置按钮是否可以被点击
android:layout_gravity 设置按钮的位置
android:gravity 设置文字的位置

以下用实例来讲解:

XML文件为:

<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.example.pylearn_01_1.MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自古一楼没卵用" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="使用XML布局"
        android:textColor="@android:color/white"
        android:textSize="50sp" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/background_dark" />

    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="可远观而不可亵玩"
        android:enabled="false" />

    <Button
        android:id="@+id/btn6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用layout_gravity让按钮居中"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/btn7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用gravity让文字居中"
        android:gravity="center" />

</LinearLayout>

java文件为:

package com.example.pylearn_01_1;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends Activity {

    /* pylearn_01_1
     * Button基本属性与方法
     */
    Button btn3;

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

        btn3=(Button)findViewById(R.id.btn3);

        btn3.setText("使用java布局");//设置文字内容
        btn3.setTextColor(android.graphics.Color.RED);//设置文字颜色
        btn3.setTextSize(45);//设置文字大小
        btn3.setEnabled(false);//设置按钮不能被点击
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

eclipse中看xml为:

来个Button看一看

模拟器中运行结果为:

来个Button看一看

源代码在此:pylearn_01_1

3.点点更健康

Button弄出来当然不是为了当花瓶的,咱们需要通过点击它来完成一些事情。

按钮的点击事件可以使用

btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //do something
    }
});

来实现。

源代码在此:pylearn_01_2

也可以使用

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);

然后让MainActivity extends Activity implements OnClickListener实现onClick方法:

@Override
public void onClick(View v) {
    // TODO 自动生成的方法存根
    switch ( v.getId() ) {
        case R.id.btn1:
            fun_btn1();
            break;
        case R.id.btn2:
            fun_btn2();
            break;
    }
}

源代码在此:pylearn_01_3

这两种方法都可以实现按钮点击事件的处理。

4.我的Button有点多

如何实现多个Button平分天下:

使用android:layout_weight控制各个按钮的权重,然后在parent布局中控制好权重和android:weightSum。最后设置各个按钮的占比为android:layout_width="0dp"。这样就实现了按钮的多个按钮的平均分配。

<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.example.pylearn_01_1.MainActivity" >

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="4" >

        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />

        <Button
            android:id="@+id/btn4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />
    </LinearLayout>
</LinearLayout>

效果如下:

来个Button看一看

源代码在此:pylearn_01_4

5.震惊!TextView竟然...

忘记在哪看到的一句话:

能用TextView的地方就别用Button

Button能实现的基本上TextView也能实现

有兴趣可以试试:把上面所有程序中的Button换成TextView,毫无违和感。