在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();
}
};
}
具体的项目实例代码已经上传我的资源,大家可以去下载,谢谢支持。