zoukankan      html  css  js  c++  java
  • ImageButton 练习(android)

    ImageButton 是就图片按纽,指定一个图片资源,就可以,其它的使用和 Button 是一样的,这里再熟悉一下对话框的使用,代码如下

    xml 代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
        <ImageButton 
            android:id="@+id/ib1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <ImageButton 
            android:id="@+id/ib2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <ImageButton 
            android:id="@+id/ib3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <ImageButton 
            android:id="@+id/ib4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>

    java代码

    package zziss.android.imagebuttontest;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.Toast;

    public class ImageButtonTestActivity extends Activity implements android.view.View.OnClickListener
    ,android.content.DialogInterface.OnClickListener {
        /** Called when the activity is first created. */
        
        private ImageButton ib1 ;
        private ImageButton ib2 ;
        private ImageButton ib3 ;
        private ImageButton ib4 ;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ib1 = (ImageButton)this.findViewById(R.id.ib1);
            ib2 = (ImageButton)this.findViewById(R.id.ib2);
            ib3 = (ImageButton)this.findViewById(R.id.ib3);
            ib4 = (ImageButton)this.findViewById(R.id.ib4);
            
            ib1.setImageResource(R.drawable.phone);
            ib2.setImageResource(R.drawable.tetris);
            ib3.setImageResource(android.R.drawable.btn_star);
            ib4.setImageResource(android.R.drawable.sym_action_call);
            
            ib1.setOnClickListener(this);
            ib2.setOnClickListener(this);
            ib3.setOnClickListener(this);
            ib4.setOnClickListener(this);
        }
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ImageButton ib = (ImageButton)v;
            AlertDialog dlg = new AlertDialog.Builder(this)
                    .setTitle("按纽点击")
                    .setMessage(Integer.toString(ib.getId()))
                    .setPositiveButton("确定",this )
                    .setNegativeButton("取消",this)
                    .create();
            dlg.show();
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            if (which == DialogInterface.BUTTON_POSITIVE)
            {
            Toast toast = Toast.makeText(this, "对话框", Toast.LENGTH_LONG);
            toast.show();
            }
            else
            {
            Toast toast = Toast.makeText(this, "对话框222", Toast.LENGTH_LONG);
            toast.show();
            }    
        }
        
        
        
        
    }
  • 相关阅读:
    Android中TextView设置下划线
    BottomSheetDialogFragment 如何设置高度和禁止滑动(Kotlin)
    [iOS]使用GCD创建定时器
    [iOS]定时器NSTimer、CADisplayLink的内存管理
    [iOS]dispatch_after()中self和weakself的使用
    [Flutter]在Mac上安装Flutter运行环境
    wx小程序反编译为js代码
    Android | 玩转AppBarLayout,设置scrollFlags滑动属性详解
    玩转微信 | 炫酷的聊天满屏掉爱心系列,赶紧收藏
    Android使用更简单的方式实现滑块拼图验证码功能
  • 原文地址:https://www.cnblogs.com/zziss/p/2305180.html
Copyright © 2011-2022 走看看