Button基本功能总结!

今天学了安卓按钮的一些基本方法,可能不是很全面立面包含按钮的5种事件创建方法和图片按钮的制作方法等!

package com.example.guowen_button;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;
	private Button btn5;
	private Button btn6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //方式一==========================================================
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "方式一正确!", Toast.LENGTH_LONG).show();
			}});
        //方式二==============================================================
        btn2=(Button)findViewById(R.id.button2);
        btn2.setOnClickListener(this);
        //方式三============================================================================//
        btn3=(Button)findViewById(R.id.button3);
        View.OnClickListener listener =new OnClickListener(){
    		@Override
    		public void onClick(View arg0) {
    			Toast.makeText(MainActivity.this,"方式三正确",Toast.LENGTH_SHORT).show();
    		}
    	};
        btn3.setOnClickListener(listener);
        //==================================================================================
        //方式四==============================================================================
        btn4=(Button)findViewById(R.id.button4);
        btn4.setOnClickListener(new MyOnClickListener());
        //方式五============================================================================
        btn5=(Button)findViewById(R.id.button5);
        btn6=(Button)findViewById(R.id.button6);
        
    }
    //自定义方式
    public void clickMe(View v)
    {
    	Toast.makeText(this,"方式五正确",Toast.LENGTH_SHORT).show();	
    }

    //菜单的构建方法========================================================
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
        
    }

//方式二实现的单击事件=====================================================================
	@Override
	public void onClick(View v) {
		switch(v.getId())
		{
		case R.id.button2:
			Toast.makeText(MainActivity.this, "方式二正确", Toast.LENGTH_SHORT).show();
		break;
		
		}
	}
	//方式四的事件=========================================================================
	  class MyOnClickListener implements View.OnClickListener
	  {
		@Override
		public void onClick(View v) 
		{
			Toast.makeText(MainActivity.this, "方式四成功", Toast.LENGTH_LONG).show();
		}
	  }
    
}
xml部分================================================================================================================================================
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="方式一" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:text="方式二" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button2"
        android:text="方式三" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button3"
        android:text="方式四" />
    <Button
	    android:id="@+id/button5"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@id/button4"
	    android:layout_alignLeft="@id/button1"
	    android:onClick="clickMe"
	    android:text="方式五"
	   />

    <Button
        android:id="@+id/button6"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button5"
        android:layout_below="@+id/button5"
        android:drawableLeft="@drawable/ic_launcher"
        android:text="方式六" />

    <Button
        android:id="@+id/button7"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button6"
        android:layout_centerHorizontal="true"
        android:background="@drawable/button_custom"
         />

</RelativeLayout>
效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值