日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android点击灰色背景图片,android button按键失去焦点和点击后改变背景图片

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android点击灰色背景图片,android button按键失去焦点和点击后改变背景图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

android button按鍵得到焦點和點擊后改變背景圖片

我也接受android不久,今天來給同學們分享一下button得到焦點和點擊后改變背景圖片,其實很簡單就是兩個事件setOnFocusChangeListener和setOnClickListener就OK了。

這是Activity 中的代碼。

package com.hai;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageButton;

import android.widget.TextView;

public class Main extends Activity {

private TextView mTV;

private ImageButton imgBtn;

private Button btn;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mTV = (TextView) findViewById(R.id.tv);

imgBtn= (ImageButton) findViewById(R.id.imgBtn);

btn = (Button) findViewById(R.id.Btn);

// ImageButton的的onFous事件

imgBtn.setOnFocusChangeListener(new ImageButton.OnFocusChangeListener(){

@Override

public void onFocusChange(View v, boolean hasFocus) {

// TODO Auto-generated method stub

if(hasFocus == true) {

imgBtn.setImageResource(R.drawable.shortcut_pic_focus);

mTV.setText("圖片按鈕狀態為:獲得焦點");

}else{

imgBtn.setImageResource(R.drawable.shortcut_pic_unfocus);

mTV.setText("圖片按鈕狀態為:失去焦點");

}

}

});

// ImageButton的單擊事件

imgBtn.setOnClickListener(new ImageButton.OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

imgBtn.setImageResource(R.drawable.shortcut_pic_focus);

mTV.setText("圖片按鈕狀態為:單擊");

}

});

// 普通按鈕的單擊事件

btn.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

imgBtn.setImageResource(R.drawable.shortcut_pic_unfocus);

mTV.setText("您按的是普通按鈕!");

}

});

}

}

然后再寫一個XML文件。

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:id="@+id/tv"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:id="@+id/imgBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/shortcut_pic_unfocus"

/>

android:id="@+id/Btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button按鈕"

/>

大伙直接把代碼拿過去就可以看到效果,記得自己本地圖片.

1 樓

haiyang08101

2011-10-09

? 很好!學習了...

總結

以上是生活随笔為你收集整理的android点击灰色背景图片,android button按键失去焦点和点击后改变背景图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。