【Android笔记】(3)Button控件

首先声明,该文章为自己学习笔记,仅供参考,不保证所有文字均描述得当,欢迎指出不足和错误之处,再此感谢您的关注和阅读。

先看下和Button有关的程序吧。

1 创建Project

File->New->Android project->ex1_Button

2 设计简单的界面

Android工程建立后会自动生成一个layout/main.xml、src/com.android.example/Ex1_ButtonActivity.java

首先修改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"
    >
<!-- 这是注释,xml规定注释需要使用这种格式,以后不再解释 -->

<Button
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:id="@+id/button1"
	android:text="我是第一个按钮"
	/>
	<Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="我是第二个按钮"
        />
</LinearLayout>

这样就完成了一个按钮的界面的设计。

3 开始写程序(即对Ex1_ButtonActivity.java进行编辑)

//这个是包名,过几天查查资料,学习学习,写个文档吧
package com.android.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
//导入Button控件
import android.widget.Button;
//导入Toast控件
import android.widget.Toast;

public class Ex1_ButtonActivity extends Activity {
    /** Called when the activity is first created. */
	private Button button1 = null;
	private Button button2 = null;
	private Button button3 = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /* button=(Button)findViewById(R.id.button1);
         * 可以先理解为将button和xml的那个绑定起来吧,
         * R.id.button1这个,还记得吗?在xml里面有句话android:id="@+id/button1"
         * 对了,如果那个R出现问题了,可以Project->clean一下,就可以了
         */
        button1=(Button)findViewById(R.id.button1);
        //这个是设置响应的一种方式吧
        button1.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//这个Toast就当作弹出一个对话框,后面会专门写哥Toast的笔记的
				Toast.makeText(getApplicationContext(), "您点击了第1个按钮",Toast.LENGTH_SHORT).show();
				
			}
		});
        //
        button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new button2OnClickListener());
               
    }
    //其实二就是一的完整版吧
    class button2OnClickListener implements OnClickListener
    {

		public void onClick(View v) {
			Toast.makeText(getApplicationContext(), "您点击了第2个按钮",Toast.LENGTH_SHORT).show();
			
		}
    	
    }
}

大概就这两步就可以简单的利用button控件了。

然后阅读一下Button的文档后,发现官方给出了另一种OnClick的方法,很方便

在main.xml加入代码

<Button
	     android:layout_height="wrap_content"
	     android:layout_width="fill_parent"
	     android:text="我是第三个按钮"
	     android:onClick="selfDestruct" 
     />


在Ex1_ButtonActivity,java加入

    public void selfDestruct(View view) {
    	Toast.makeText(getApplicationContext(), "您点击了第3个按钮",Toast.LENGTH_SHORT).show();
    }


第一篇文档吧,恩。就到这

另外,推荐一个Android学习论坛吧,我也赚赚下载积分。Orz...
http://www.eoeandroid.com/?fromuser=kerenigma




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值