android4.0开发学习之Button

本文介绍如何在Android4.0开发中使用Button控件实现点击按钮后的提示功能,包括正常按钮、小按钮及切换按钮的实现,并提供详细代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Android4.0的开发当中Button控件主要有三种正常大小按钮,偏小按钮与切换按钮,实例如下:


下面实现点击按钮提示,效果如下:


具体是实现代码如下:

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Buttons</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    
	<string name="buttons_normal">正常按钮</string>
	<string name="buttons_small">小按钮</string>
	<string name="buttons_toggle">切换按钮</string>
	
</resources>
布局文件main.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"
    tools:context="com.lzx.buttons.MainActivity" >

	<LinearLayout
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:orientation="vertical">
	        
	        <!-- Regular sized buttons -->
	        <Button android:id="@+id/button_normal"
	            android:text="@string/buttons_normal"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content" />
	
	        <!-- Small buttons -->
	        <Button android:id="@+id/button_small"
	            style="?android:attr/buttonStyleSmall"
	            android:text="@string/buttons_small"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content" />
	
	        <ToggleButton android:id="@+id/button_toggle"
	            android:text="@string/buttons_toggle"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content" />
	            
	    </LinearLayout>

</LinearLayout>
实现Activity代码

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends Activity {

	private Button button_normal,button_small;//正常按钮和小按钮
	private ToggleButton button_toggle;//切换按钮
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button_normal = (Button) findViewById(R.id.button_normal);
		button_small = (Button) findViewById(R.id.button_small);
		button_toggle = (ToggleButton) findViewById(R.id.button_toggle);
		
		button_normal.setOnClickListener(listener);
		button_small.setOnClickListener(listener);
		button_toggle.setOnCheckedChangeListener(checkedChangeListener);
	}
	
	//设置按钮的监听器
	private OnClickListener listener = new OnClickListener() 
	{
		@Override
		public void onClick(View v) 
		{
			switch (v.getId()) {
			case R.id.button_normal:
				Toast.makeText(MainActivity.this, "你点击了正常按钮!", Toast.LENGTH_SHORT).show();
				break;

			case R.id.button_small:
				Toast.makeText(MainActivity.this, "你点击了小按钮", Toast.LENGTH_SHORT).show();
				break;
			}
		}
	};
	//设置切换按钮监听
	private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() 
	{
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
		{
			if(isChecked)
				Toast.makeText(MainActivity.this, "你打开了按钮!", Toast.LENGTH_SHORT).show();
			else if(!isChecked)
				Toast.makeText(MainActivity.this, "你关闭了按钮!", Toast.LENGTH_SHORT).show();
		}
	};
}


具体的项目实例代码已经上传我的资源,大家可以去下载,谢谢支持。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值