Android 控件开发之Button
2015-07-27 10:01
519 查看
摘自: http://blog.csdn.net/sun6255028/article/details/6685003
Button,就是按钮,是android中应用最多的组件之一
Button的显示效果图:
本文程序的main.xml
[cpp] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2"/>
</LinearLayout>
本程序的string.xml
[cpp] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="btn1">确认</string>
<string name="btn2">取消</string>
<string name="app_name">Button</string>
</resources>
本程序的java文件
[cpp] view
plaincopy
package com.sx.Button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ButtonActivity extends Activity
{
private Button btn1 = null;
private Button btn2 = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new ButtonClicked());
btn2 = (Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new ButtonClicked());
}
//创建Button监听器
class ButtonClicked implements OnClickListener
{
public void onClick(View v)
{
switch(v.getId())
{
case R.id.btn1:
//系统输出信息
System.out.println("你点击了确定按钮");
//使用Toast类输出信息
Toast.makeText(ButtonActivity.this, "你点击了确定按钮", Toast.LENGTH_LONG).show();
break;
case R.id.btn2:
System.out.println("你点击了取消按钮");
Toast.makeText(ButtonActivity.this, "你点击了取消按钮", Toast.LENGTH_LONG).show();
break;
}
}
}
}
sun6255028的专栏
Button,就是按钮,是android中应用最多的组件之一
Button的显示效果图:
本文程序的main.xml
[cpp] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2"/>
</LinearLayout>
本程序的string.xml
[cpp] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="btn1">确认</string>
<string name="btn2">取消</string>
<string name="app_name">Button</string>
</resources>
本程序的java文件
[cpp] view
plaincopy
package com.sx.Button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ButtonActivity extends Activity
{
private Button btn1 = null;
private Button btn2 = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new ButtonClicked());
btn2 = (Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new ButtonClicked());
}
//创建Button监听器
class ButtonClicked implements OnClickListener
{
public void onClick(View v)
{
switch(v.getId())
{
case R.id.btn1:
//系统输出信息
System.out.println("你点击了确定按钮");
//使用Toast类输出信息
Toast.makeText(ButtonActivity.this, "你点击了确定按钮", Toast.LENGTH_LONG).show();
break;
case R.id.btn2:
System.out.println("你点击了取消按钮");
Toast.makeText(ButtonActivity.this, "你点击了取消按钮", Toast.LENGTH_LONG).show();
break;
}
}
}
}
相关文章推荐
- Android DatePickerDialog 设置日期范围
- android 永久NotitleBar
- Android中的三种XML解析方式的差异和比较
- Android bitmap高效显示和优化
- Android4.3 BLE蓝牙初步
- Android(java)学习笔记130:ProgressBar使用的
- Android开发之文件命名规范
- Android性能优化:谈话Bitmap内存管理和优化
- Android——开发框架的使用说明
- android 自定义dialog并实现失去焦点(背景透明)的功能
- ArcGIS Runtime for Android开发教程V2.0(9)基础篇-----查询检索
- ArcGIS Runtime for Android开发教程V2.0(7)基础篇---空间要素可视化2
- ArcGIS Runtime for Android开发教程V2.0(8)基础篇-----地图事件
- Android命令行测试BT WiFi Sensor工作状态
- ArcGIS Runtime for Android开发教程V2.0(6)基础篇---空间要素可视化
- ArcGIS Runtime for Android开发教程V2.0(5)基础篇---图层
- ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView
- AndroidJNI.SetCharField 设置字符域
- Android恶意代码分析与渗透测试
- android 读取 AndroidManifest.xml 中的数据:版本号、应用名称、自定义K-V数据(meta-data)