ImageButton 練習(android)

來源:互聯網
上載者:User

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();
        }    
    }
    
    
    
    
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.