码迷,mamicode.com
首页 > 移动开发 > 详细

步步为营_Android开发课[17]_用户界面之Button(按钮)

时间:2015-04-07 19:49:05      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:button   android   用户界面   实例   

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305


  • 主题:用户界面之Button(按钮)

Button和ImageButton控件实例:

activity_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"
    >
    <Button 
        android:id="@+id/btn"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="我是Button"
    />
    <ImageButton 
        android:id="@+id/imgbtn"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/myimage"
    />
</LinearLayout>

在MainActivity.java里给它实例化并加上按钮点击的监听事件:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends Activity {

    private Button btn;
    private ImageButton imgbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化EditText
        btn = (Button)findViewById(R.id.btn);
        imgbtn = (ImageButton)findViewById(R.id.imgbtn);

        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Log.v("------------", "我是Button");
            }

        });

        imgbtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Log.v("------------", "我是ImageButton");
            }

        });
    }
}

运行结果:

技术分享

点击第一个按钮:
Logcat窗口显示
技术分享

点击第二个按钮:
Logcat窗口显示
技术分享

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305

步步为营_Android开发课[17]_用户界面之Button(按钮)

标签:button   android   用户界面   实例   

原文地址:http://blog.csdn.net/y18334702058/article/details/44923057

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!