Android学习 12-> 按钮Button

       button按钮,和所有控件一样,在布局文件拖拽一个按钮,在代码文件中通过id过去即可,通常请求下使用button按钮,我们会为它写上一个监听器,用来控制按下button以后的操作,一般都是跳转另一个界面或者弹出某个信息。

      要实现事件监听就要在xml中添加Button按钮时添加 android:onClick=" __ "即设置点击事件的方法;在java程序中
<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">public void  __ (View v) {
		Intent intent = new Intent(this,   .class);
		startActivity(intent);
	}</span></span></span>

     通过Intent来实现事件的跳转,用startActivity()来启动相应的事件。

   

    android:shadowColor   //阴影颜色

    android:shadowRadius   //阴影半径

    android:shadowDx   //阴影x坐标位移

    android:shadowDy   //阴影y坐标位移

    android:background   //带背景或者设置属性   若是android:background="@drawable/ped"将设置按钮的背景为图片资源;若是android:background="@android:color/black"则是将背景设置为系统颜色。

     实现方法:可以用实现接口或匿名内部类的形式;由于匿名内部类用习惯了,此处以匿名内部类的方法操作

   实现如图所示:

     

 

     其xml代码如下:

<span style="font-size:18px;"><ScrollView 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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onLinerLayoutClickView"
            android:text="线型布局" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onFrameCheckView"
            android:text="单帧布局" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onRelativeCheckView"
            android:text="相对布局" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onTableCheckView"
            android:text="表格布局" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioCheckView"
            android:text="单选,多选布局" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onGridCheckView"
            android:text="网格布局" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onLoginCheckView"
            android:text="登陆练习" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onSendCheckView"
            android:text="传参练习" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onListsViewCheckView"
            android:text="ListsView学习" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onSimpleListViewCheckView"
            android:text="SimpleListView学习" />
    </LinearLayout>

</ScrollView></span>


    其Java运行方法为:

<span style="font-size:18px;">package com.sc.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.sc.android.activity.SendActivity;
import com.sc.android.ui.component.LoignViewActivity;
import com.sc.android.ui.component.RadioButtonCheckBoxActivity;
import com.sc.android.ui.layout.FrameLayoutActivity;
import com.sc.android.ui.layout.GridLayoutActivity;
import com.sc.android.ui.layout.LinerLayoutActivity;
import com.sc.android.ui.layout.RelativeLayoutActivity;
import com.sc.android.ui.layout.TableLayoutActivity;
import com.sc.android.ui.listview.ListsView;
import com.sc.android.ui.listview.SimpleListView;

public class MainActivity extends Activity {

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

	public void onLinerLayoutClickView(View v) {
		Intent intent = new Intent(this, LinerLayoutActivity.class);
		startActivity(intent);
	}

	public void onRelativeCheckView(View v) {
		Intent intent = new Intent(this, RelativeLayoutActivity.class);
		startActivity(intent);
	}

	public void onTableCheckView(View v) {
		Intent intent = new Intent(this, TableLayoutActivity.class);
		startActivity(intent);
	}

	public void onGridCheckView(View v) {
		Intent intent = new Intent(this, GridLayoutActivity.class);
		startActivity(intent);
	}

	public void onFrameCheckView(View v) {
		Intent intent = new Intent(this, FrameLayoutActivity.class);
		startActivity(intent);
	}

	public void onRadioCheckView(View v) {
		Intent intent = new Intent(this, RadioButtonCheckBoxActivity.class);
		startActivity(intent);
	}

	public void onLoginCheckView(View v) {
		Intent intent = new Intent(this, LoignViewActivity.class);
		startActivity(intent);
	}

	public void onSendCheckView(View v) {
		Intent intent = new Intent(this, SendActivity.class);
		startActivity(intent);
	}

	public void onListsViewCheckView(View v) {
		Intent intent = new Intent(this, ListsView.class);
		startActivity(intent);
	}
	public void onSimpleListViewCheckView(View v) {
		Intent intent = new Intent(this, SimpleListView.class);
		startActivity(intent);
	}
}
</span>

     当然,以上的类是已经建立好的,在AndroidMafest.xml文件中也注册登记好了的。这样就可以实现点击相应的按钮直接跳转到相应的界面当中去了。
  

     自定义Button:在Drawable文件下建立一个xml文件,其xml格式如下:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   
    <item android:drawable="@android:color/holo_orange_dark" android:state_pressed="true"/>
    <item android:drawable="@android:color/holo_green_dark"/>
    
</selector></span>

     在上面的设置中也可以选择其他的:上面的state_pressed为按下时的状态;state_focused是选择焦点;state_enabled为单个属性;state_cheeked 为选中等。

   在设置Button的属性中将背景设为应用该xml文件就可以实现其相应的自定义格式了,当然之上的是引用系统颜色变化来实现的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值