導航:首頁 > 操作系統 > android中imagebutton

android中imagebutton

發布時間:2023-06-30 12:33:49

android中帶圖標的按鈕(ImageButton)怎麼用

除了Android系統自帶的Button按鈕以外,還提供了帶圖標的按鈕ImageButton
要製作帶圖標的按鈕,首先要在布局文件中定義ImageButton,然後通過setImageDrawable方法來設置要顯示的圖標。
注意:
我們可以在布局文件中就直接設置按鈕的圖標,如
android:src=」@drawable/icon1″
我們也可以在程序中設置自定義圖標
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
我們還可以使用系統自帶的圖標
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));
設置完按鈕的圖標後,需要為按鈕設置監聽setOnClickListener,以此捕獲事件並處理
下面的例子講述的是由4個圖標按鈕組成的布局,其中三個按鈕的圖標是自定義的,第四個按鈕的圖標是系統的,當點擊按鈕1的時候,彈出dialog,當點擊按鈕2的時候,點擊確定後,可以將按鈕2的圖標變成按鈕3的圖標,當點擊按鈕3的時候,按鈕3的圖標變成了系統打電話的圖標,點擊按鈕4,顯示一個提示dialog
ImageButtonTest.java源代碼
package org.loulijun.imagebutton;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class ImageButtonTest extends Activity {
/** Called when the activity is first created. */
TextView textview;
ImageButton imgbtn1;
ImageButton imgbtn2;
ImageButton imgbtn3;
ImageButton imgbtn4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textview=(TextView)findViewById(R.id.textview);
//分別取得4個ImageButton對象
imgbtn1=(ImageButton)findViewById(R.id.imagebutton1);
imgbtn2=(ImageButton)findViewById(R.id.imagebutton2);
imgbtn3=(ImageButton)findViewById(R.id.imagebutton3);
imgbtn4=(ImageButton)findViewById(R.id.imagebutton4);

//分別為ImageButton設置圖標
//imgbtn1已經在main.xml布局中設置了圖標,所以就不在這里設置了(設置圖標即可在程序中設置,也可在布局文件中設置)
imgbtn2.setImageDrawable(getResources().getDrawable(R.drawable.icon));//在程序中設置圖標
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));//設置系統圖標

//下面為各個按鈕設置事件監聽
imgbtn1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton1")
.setPositiveButton("確定",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//相應的處理操作
}
}).create();
dialog.show();
}

});

imgbtn2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton2,我要使用ImageButton3的圖標")
.setPositiveButton("確定",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
imgbtn2.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
}
}).create();
dialog.show();
}

});

imgbtn3.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton3,我想使用系統打電話的圖標")
.setPositiveButton("確定",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
imgbtn3.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_action_call));
}
}).create();
dialog.show();
}

});

imgbtn4.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是使用的系統圖標")
.setPositiveButton("確定",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//相應的處理操作
}
}).create();
dialog.show();
}

});
}
}

布局文件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"
>
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ImageButton測試案例"
/>
<ImageButton
android:id="@+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon1"
/>
<ImageButton
android:id="@+id/imagebutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/imagebutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/imagebutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

㈡ android開發中如何設置ImageButton的大小

在Android開發中,java代碼用於將zoom設置為fitxy,從而使類能夠繼承uibutton、重寫button和設置ImageView size來設置imagebutton的大小,具體Java設置代碼如下:

<ImageButton

android:id="@+id/btn_delete"

android:layout_width="48dp"

android:layout_height="80dp"

android:scaleType="fitXY"

android:background="@drawable/bg_sms"/>

此外,Android還包括一些C/C++庫,這些庫可以被Android系統中的不同組件使用,他們通過Android應用程序框架為開發人員提供服務。

(2)android中imagebutton擴展閱讀:

Android應用軟體開發語言有C語言等多種語言,但主流的開發語言是java語言,這使得界面的功能有了無盡的變化,增加軟體交互的可能性是Java的最大特點。

所有的Android應用程序都是用Java語言編寫的,用java語言開發的軟體程序庫、資料庫和運行庫是Android移動軟體的主要特點。

Java語言是增長最快的編程語言,它具有面向對象的特點,它相對容易理解,它的顯著特點是簡單,它繼承了C++語言的高級本質,它是計算機程序設計語言發展的一大進步,Java語言有一個獨立的體系結構,可以在任何系統中任意運行。

閱讀全文

與android中imagebutton相關的資料

熱點內容
java函數return 瀏覽:25
鄭州app積分商城哪裡好 瀏覽:610
7za命令解壓zip 瀏覽:906
硬碟加密無法進入系統 瀏覽:560
四葉花演算法 瀏覽:741
寶寶濾鏡app哪裡下載 瀏覽:1002
java保護代碼 瀏覽:806
游戲海外伺服器什麼意思 瀏覽:568
快手網紅程序員排名 瀏覽:99
首先會通過什麼尋找伺服器的ip地址 瀏覽:199
網頁代碼加密解碼 瀏覽:285
wyks1ms文件夾 瀏覽:93
什麼app可以看柯南高清 瀏覽:425
加密的盤文件恢復 瀏覽:22
絕對路徑能查找隱藏文件夾嗎 瀏覽:568
做抖音相冊用什麼圖片app 瀏覽:726
pc怎麼打開sftp伺服器 瀏覽:998
中長跑用什麼app 瀏覽:943
hlddz是什麼文件夾怎麼換盤 瀏覽:458
oppo主題編譯器下載 瀏覽:807