Android之Button,ImageButton

普通按钮和图片按钮

1.布局文件

<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" >

<TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<Button
    android:id="@+id/normal_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/normal_text"/>
<Button
    android:id="@+id/small_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/small_text"/>
<ToggleButton
    android:id="@+id/toggle_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/toggle_text"/>
<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/red"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/purple"
    android:text="@string/button_text"/>

</LinearLayout>

2.代码文件
package com.example.button;

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

public class MainActivity extends Activity {

	Button normalButton,smalButton,toggButton;
	TextView textView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		normalButton = (Button)findViewById(R.id.normal_button);
		smalButton = (Button)findViewById(R.id.small_button);
		toggButton = (Button)findViewById(R.id.toggle_button);
		textView = (TextView) findViewById(R.id.text);
		normalButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				textView.setText("Normall button is clicked!");	
			}
		});
		smalButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				textView.setText("small button is clicked!");	
			}
		});
		toggButton.setOnClickListener(new OnClickListener() {
	
	       @Override
	       public void onClick(View v) {
		       textView.setText("toggle button is clicked!");	
	       }
        });
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值