您的位置:首页 > 移动开发 > Android开发

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

2015-04-07 17:15 369 查看
Focus on technology, enjoy life!—— QQ:804212028

浏览链接:/article/1513032.html

主题:用户界面之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

浏览链接:/article/1513032.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: