當前位置:首頁 » 安卓系統 » androidbuttonimage

androidbuttonimage

發布時間: 2023-05-25 23:04:51

❶ 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中的Button怎麼在點擊更換背景點擊後又恢復原來的背景

只需要給Button配置一個Selector背景選擇器即可實現。
1.創建mylist_view.xml文件
首先在res目錄下新建drawable文件夾,再在新建的drawable文件夾中新建mylist_view.xml,其目錄結構為:res/drawable/mylist_view.xml。

2.根據具體需求編輯mylist_view.xml文件
新建mylist_view.xml文件後,在沒有添加任何屬性時其內部代碼結構為:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>

3.下面就可以根據項目需求,在其內部定義為自己想要的樣式了,主要屬性如下:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默認時的背景圖片-->
<item android:drawable="@drawable/pic1" />
<!-- 沒有焦點時的背景圖片 -->
<item android:state_window_focused="false" android:drawable="@drawable/pic1" />
<!-- 非觸摸模式下獲得焦點並單擊時的背景圖片 -->
<item android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic2" />
<!-- 觸摸模式下單擊時的背景圖片-->
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pic3" />
<!--選中時的圖片背景-->
<item android:state_selected="true" android:drawable="@drawable/pic4" />
<!--獲得焦點時的圖片背景-->
<item android:state_focused="true" android:drawable="@drawable/pic5" />
</selector>

4.引用mylist_view.xml文件
android:background="@drawable/mylist_view"

❸ android中,如何做圓形的button按鈕

自己繪制圓形的圖片,然後在button布局裡面用BackgroundDrawable設置為button背景。android中是不帶圓形的button的

❹ Android中如何判斷兩個Button的背景圖片是否相等

android 根據button不同狀態顯示不同(背景)圖片
2011-02-24 | nedvedno1 | 轉藏(19)
網上的思路不錯,我只做了基於xml的實現。先來貼一段網上的經典code:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/button_add" />
<item android:state_pressed="true" android:drawable="@drawable/button_add_pressed" />
<item android:state_focused="true" android:drawable="@drawable/button_add_pressed" />
<item android:drawable="@drawable/button_add" />
</selector>
這個文件放在drawable目錄下面。命名為button_add_x.xml
使用的時候
<ImageButton
android:id="@+id/ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:src="@drawable/button_add_x"
>
</ImageButton>
按照這種方法 我試了多次,發現根本不能生效。後來才發現,這方法是針對ImageButton的,於是查了一下Button和ImageButton的區別,如果區別不大,我就打算把我自己的Button換成ImageButton了,但結果令我失望了,區別還不小呢,主要是這2種控制項對於實現onClick方法的機制不同,想想就復雜,如果貿然換了,編譯的時候恐怕要出現很多error。
再著,這方法並沒有改變button的background,而我們基於button去實現圖片一般都是直接+background的,對么?
下面貼我的code:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/xxx1" />
<item android:state_pressed="true" android:drawable="@drawable/xxx2" />
<item android:state_focused="true" android:drawable="@drawable/xxx3" />
<-- 這里還可以加N多效果和動作 只要你用的到 -->
<item android:drawable="@drawable/xxx4" />
</selector>這個文件沒有不同,起名為button_add_x.xml 放在drawable下。
使用的時候有所不同
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_add_x" <---這里background直接指向剛剛編寫的文件
//這行不需要--->android:src="@drawable/button_add_x"
>
</Button>ok,編譯 push後就生效了。如果你原來的background指向的是一張.png圖片的話,那麼把它換成一個自己製作的文件就好。

❺ android 按鈕設置點擊更換imagebutton背景

button.setBackgroundResource(R.drawable.beijing1);
上面是改變按鈕背景的代碼
可以做兩組圖片,分別為button1的選中和為選擇狀態、button2的選中和為選擇狀態,讓後再button1和button2的點擊事件中,對兩個按鈕的背景進行改變,就可以了

❻ android點擊以後改變button的顏色

android點擊以後改變button的顏色的方法為:
1、新建 drawable/button_font_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff"/>
<item android:state_focused="true" android:color="#fff"/>
<item android:color="#000" />
</selector>
2、添加至需要的xml里即可
<android:textColor="@drawable/button_font_style">

❼ android中button有幾種狀態

Android中,button按鈕通常有三個狀態:

1. normal(正常狀態);
2. focus(焦點狀態);
3. pressed(按下狀態)
4. selected(選中狀態)

注意:按下後未松開前是pressed,表示按下。
松開後當前項目獲得焦點,是focused。
focused的項只有一個,selected是當選中該按鈕時顯示的狀態。

❽ Android 當我點擊一個Button後,當前的Activity背景改變(顏色或者圖片),應該怎麼寫

直接設置斗運資源文件:button.setBackground(R.id.image);
設置空伏梁drawable作廳野為背景button.setBackgroundDrawable(Drawable drawable);

❾ 在android中按鈕共分為幾種

從控制項來說分為2種:button(一般按鈕)和ImageButton(圖片按鈕);
但是大部分時候,開發者是可以通過各種方式自定義按鈕,這樣的話,界面呈現出來的按鈕是多種多樣的;
TextView,view等等,很多控制項其實都可以拿來當按鈕使用;
此外,還有包括ToggleButton,單選按鈕,多選按鈕等這些都屬於是功能比較專一的特殊按鈕了;
我想你只有對android比較了解的情況下,才可能理解深一些吧!

❿ android裡面button的背景圖片大小怎麼能根據button大小縮放

<p>可以在ps中編輯到合適的解析度
</p><p>可以把Button組件和一個ImageView組件放在一個FrameLayout布局中,規定好FrameLayout的尺寸,然後對上面的兩個組件的寬和高都設置為match_parent</p><p>可以把上述兩個組件換成一個ImageButton組件</p><p>如果需要圖片的切換,可以考慮ImageSwitcher組件</p><p>圖片縮放演算法,縮放好後,動態為ImageView添加圖片
</p><p>
</p>

熱點內容
聽ti密碼是多少 發布:2025-02-12 08:22:15 瀏覽:288
淘寶上傳視頻憑證 發布:2025-02-12 08:06:46 瀏覽:878
java畫 發布:2025-02-12 08:01:00 瀏覽:549
光遇安卓官服是在哪裡下載 發布:2025-02-12 07:47:47 瀏覽:648
安卓手機如何關閉程序打開廣告 發布:2025-02-12 07:31:06 瀏覽:469
新版影視大全不能緩存 發布:2025-02-12 07:31:04 瀏覽:976
sql兩個欄位in 發布:2025-02-12 07:29:45 瀏覽:771
漂亮網站源碼 發布:2025-02-12 07:26:40 瀏覽:760
執行腳本前 發布:2025-02-12 07:14:49 瀏覽:472
android天氣預報介面 發布:2025-02-12 07:12:43 瀏覽:703