03 Button 控件

<?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:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    <Button 
        android:id="@+id/btn1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="点我" 
        />
    <Button 
        android:id="@+id/btn2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="点我2" 
        />
</LinearLayout>
package com.szy.button.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.Toast;

public class MainActivity extends Activity
{
    private Button btn1=null;
    private Button btn2=null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1=(Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);
        btn1.setOnClickListener(listener);
        btn2.setOnClickListener(listener);
        
    }
    
    private OnClickListener listener=new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Button btn=(Button)v;
            switch (btn.getId())
            {
            case R.id.btn1:
                Toast.makeText(MainActivity.this, "你点击了按钮", Toast.LENGTH_LONG).show();
                break;
            case R.id.btn2:
                System.out.println("你点击了Button2");
                break;
            }
        }
    };
}

 

posted @ 2014-04-28 16:38  阿黄的苹果  阅读(116)  评论(0编辑  收藏  举报