imagebutton和button的區別
Imagebutton 繼承 Imageview,就是用一個圖標代表了一些文字,它沒Android:text屬性。它由Android:src指定圖標的位置
android:src="@drawable/back"
Button 繼承 Textview,所以TextView的一些屬性也適用于Button控件。
Button把圖片當作背景與放在ImageButton/ImageView中的效果是不一樣的。
android中帶圖標的按鈕(ImageButton)怎么用
除了Android系統自帶的Button按鈕以外,還提供了帶圖標的按鈕ImageButton
要制作帶圖標的按鈕,首先要在布局文件中定義ImageButton,然后通過tImageDrawable方法來設置要顯示的圖標。
注意:
我們可以在布局文件中就直接設置按鈕的圖標,如
android:src=”@drawable/icon1″
我們也可以在程序中設置自定義圖標
imgbtn3.tImageDrawable(getResources().getDrawable(R.drawable.icon2));
我們還可以使用系統自帶的圖標
imgbtn4.tImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));
設置完按鈕的圖標后,需要為按鈕設置監聽tOnClickListener,以此捕獲事件并處理
下面的例子講述的是由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);
tContentView(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.tImageDrawable(getResources().getDrawable(R.drawable.icon));//在程序中設置圖標
imgbtn3.tImageDrawable(getResources().getDrawable(R.drawable.icon2));
imgbtn4.tImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));//設置系統圖標
//下面為各個按鈕設置事件監聽
imgbtn1.tOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.tTitle("提示")
.tMessage("我是ImageButton1")
.tPositiveButton("確定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//相應的處理操作
}
}).create();
dialog.show();
}
});
imgbtn2.tOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.tTitle("提示")
.tMessage("我是ImageButton2,我要使用ImageButton3的圖標")
.tPositiveButton("確定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
imgbtn2.tImageDrawable(getResources().getDrawable(R.drawable.icon2));
}
}).create();
dialog.show();
}
});
imgbtn3.tOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.tTitle("提示")
.tMessage("我是ImageButton3,我想使用系統打電話的圖標")
.tPositiveButton("確定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
imgbtn3.tImageDrawable(getResources().getDrawable(android.R.drawable.sym_action_call));
}
}).create();
dialog.show();
}
});
imgbtn4.tOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.tTitle("提示")
.tMessage("我是使用的系統圖標")
.tPositiveButton("確定",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應用程序框架為開發人員提供服務。
擴展資料:
Android應用軟件開發語言有C語言等多種語言,但主流的開發語言是java語言,這使得界面的功能有了無盡的變化,增加軟件交互的可能性是Java的最大特點。
所有的Android應用程序都是用Java語言編寫的,用java語言開發的軟件程序庫、數據庫和運行庫是Android移動軟件的主要特點。
Java語言是增長最快的編程語言,它具有面向對象的特點,它相對容易理解,它的顯著特點是簡單,它繼承了C++語言的高級本質,它是計算機程序設計語言發展的一大進步,Java語言有一個獨立的體系結構,可以在任何系統中任意運行。
參考資料來源:
百度百科-Android
百度百科-軟件開發
android中怎么添加imagebutton圖標
在layout目錄下的main.xml里加一個ImageButton,具體代碼如下:
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
//android:src="這里是給ImageButton加一個圖片做這個imagebutton的圖標"
在Activity里面監聽這個ImageButton時間具體代碼如下:
ImageButton
imageButton
=
(ImageButton)
findViewById(R.id.imageButton1);
imageButton.tOnClickListener(this);
Android框架 ImageButton如何實現 按住和松開時的事件
這個可以用onTouch來實現,touch事件有手指按下,移動,放開的相關api。
關于Touch事件的說明:
1.關于事件構成
在Android中,事件主要包括點按、長按、拖拽、滑動等,點按又包括單擊和雙擊,另外還包括單指操作和多指操作。所有這些都構成了Android中的事件響應。總的來說,所有的事件都由如下三個部分作為基礎:
1.按下(ACTION_DOWN)
2.移動(ACTION_MOVE)
3.抬起(ACTION_UP)
所有的操作事件首先必須執行的是按下操作(ACTIONDOWN),之后所有的操作都是以按下操作作為前提,當按下操作完成后,接下來可能是一段移動(ACTIONMOVE)然后抬起(ACTION_UP),或者是按下操作執行完成后沒有移動就直接抬起。這一系列的動作在Android中都可以進行控制。
2.事件的處理API
在View和ViewGroup中都存在dispatchTouchEvent和onTouchEvent方法,但是在ViewGroup中還有一個onInterceptTouchEvent方法,在Android中,所有的事件都是從開始經過傳遞到完成事件的消費,這些方法的返回值就決定了某一事件是否是繼續往下傳,還是被攔截了,或是被消費了。
public boolean dispatchTouchEvent(MotionEvent event)
public boolean onTouchEvent(MotionEvent event)
public boolean onInterceptTouchEvent(MotionEvent event)
3.事件處理API的說明
dispatchTouchEvent方法用于事件的分發,Android中所有的事件都必須經過這個方法的分發,然后決定是自身消費當前事件還是繼續往下分發給子控件處理。返回true表示不繼續分發,事件沒有被消費。返回fal則繼續往下分發,如果是ViewGroup則分發給onInterceptTouchEvent進行判斷是否攔截該事件。
onTouchEvent方法用于事件的處理,返回true表示消費處理當前事件,返回fal則不處理,交給子控件進行繼續分發。
onInterceptTouchEvent是ViewGroup中才有的方法,View中沒有,它的作用是負責事件的攔截,返回true的時候表示攔截當前事件,不繼續往下分發,交給自身的onTouchEvent進行處理。返回fal則不攔截,繼續往下傳。
說明Image、ImageButton和ImageMap控件的區別
Image僅僅是圖片控件,展示圖片所用,對應HTML的<img />標簽
ImageButton是一個圖片按鈕,具有按鈕的功能,對應HTML的<input type="image" />標簽
ImageMap叫做圖片熱點,單擊圖片上不同區域可以進行不同的動作,對應HTML的<map /><area /><img />三個標簽
希望對您有幫助~
By Billskate