android应用开发揭秘examples_04-12笔记(Button的使用)
2013-02-26 15:59
671 查看
演示效果:
前面例子几乎每一个都有button,所以这里也不多解释了,上代码:
package com.yarin.android.Examples_04_12;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Activity01 extends Activity
{
//声明按钮
Button m_Button1,m_Button2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得Button对象
m_Button1=(Button)findViewById(R.id.Button1);
m_Button2=(Button)findViewById(R.id.Button2);
//设置按钮嗦显示的文字
m_Button1.setText("开始");
m_Button2.setText("退出");
//设置按钮的宽度
m_Button1.setWidth(150);
m_Button2.setWidth(100);
//设置文字的颜色
m_Button1.setTextColor(Color.GREEN);
m_Button2.setTextColor(Color.RED);
//设置文字的大小尺寸
m_Button1.setTextSize(30);
m_Button2.setTextSize(20);
//m_Button1.setBackgroundColor(Color.BLUE);
//设置按钮的事件监听
m_Button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
//处理按钮事件
//产生一个Toast
//m_Button1.getText()得到按钮显示的内容
Toast toast = Toast.makeText(Activity01.this, "你点击了“"+m_Button1.getText()+"”按钮!", Toast.LENGTH_LONG);
//设置toast显示的位置
toast.setGravity(Gravity.TOP, 0, 150);
//显示该Toast
toast.show();
}
});
m_Button2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
//处理按钮事件
Activity01.this.finish();
}
});
}
}
------------------------ main.xml ---------------------------------
<?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"
>
<TextView
android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
<Button
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
</LinearLayout>
前面例子几乎每一个都有button,所以这里也不多解释了,上代码:
package com.yarin.android.Examples_04_12;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Activity01 extends Activity
{
//声明按钮
Button m_Button1,m_Button2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得Button对象
m_Button1=(Button)findViewById(R.id.Button1);
m_Button2=(Button)findViewById(R.id.Button2);
//设置按钮嗦显示的文字
m_Button1.setText("开始");
m_Button2.setText("退出");
//设置按钮的宽度
m_Button1.setWidth(150);
m_Button2.setWidth(100);
//设置文字的颜色
m_Button1.setTextColor(Color.GREEN);
m_Button2.setTextColor(Color.RED);
//设置文字的大小尺寸
m_Button1.setTextSize(30);
m_Button2.setTextSize(20);
//m_Button1.setBackgroundColor(Color.BLUE);
//设置按钮的事件监听
m_Button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
//处理按钮事件
//产生一个Toast
//m_Button1.getText()得到按钮显示的内容
Toast toast = Toast.makeText(Activity01.this, "你点击了“"+m_Button1.getText()+"”按钮!", Toast.LENGTH_LONG);
//设置toast显示的位置
toast.setGravity(Gravity.TOP, 0, 150);
//显示该Toast
toast.show();
}
});
m_Button2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
//处理按钮事件
Activity01.this.finish();
}
});
}
}
------------------------ main.xml ---------------------------------
<?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"
>
<TextView
android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
<Button
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
</LinearLayout>
相关文章推荐
- android应用开发揭秘examples_04-08笔记(CheckBox的使用)
- android应用开发揭秘examples_04-09笔记(Spinner的使用,略有修改)
- android应用开发揭秘examples_04-10笔记(AutoCompleteTextView和MultiAutoCompleteTextView使用)
- android应用开发揭秘examples_04-16笔记(ImageButton的使用)
- android应用开发揭秘examples_04-17笔记(Gallery的使用)
- android应用开发揭秘examples_04-11笔记(DatePicker,TimePicker时间日期设置,略有改动)
- android应用开发揭秘examples_04-13笔记(Menu的2种实现方式)
- android应用开发揭秘例4-6笔记(EditText的使用)
- android应用开发揭秘例example4-7笔记(RadioGroup和RadioButton的使用与改进)
- android应用开发揭秘之examples_04_24(ProgressDialog对话框中的进度条)
- android应用开发揭秘之examples_04_29(TabWidget及FrameLayout)
- android应用开发揭秘例4-3笔记(TextView的使用)
- android应用开发揭秘例4-4笔记(ListView及Adapter的使用)
- Android应用开发学习笔记04--使用布局
- android应用开发笔记---SQLite使用
- Android应用开发揭秘的第17个程序5_12_DBuffImg修改版和高仿版的源码注释
- Android 应用开发笔记 - 按钮(Button, ImageButton)
- Android应用开发揭秘的第26个程序12_2_openglcolor修改版和高仿版的源码注释
- android 应用开发揭秘,ndk使用过程总结
- Android应用开发笔记(12):Android应用的自动升级、更新模块的实现