學(xué)習(xí)啦>學(xué)習(xí)電腦>電腦技巧>

android5.0程序開發(fā)的圖片按鈕ImageButton使用教程

時間: 本達(dá)868 分享

  在android5.0程序開發(fā)中,你知道圖片按鈕控件ImageButton怎么使用嗎?下面是學(xué)習(xí)啦小編給大家整理的一些有關(guān)android5.0程序開發(fā)的圖片按鈕ImageButton使用教程,希望對大家有幫助!

  android5.0程序開發(fā)的圖片按鈕ImageButton使用教程

  啟動eclipse程序,新建一android5.0工程文件ImageButtonDemo。

  將圖片素材導(dǎo)入到drawable目錄中,可以復(fù)制圖片文件,在drawable目錄中進(jìn)行粘貼。注意圖片文件名不能以數(shù)字開頭,否則出錯。

  在activity_main.xml文件中加入一個TextView文本控件和兩個ImageButton圖片按鈕控件。

  <?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="hp://schemas.android.apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

  <TextView

  android:id="@+id/TextView01"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content">

  </TextView>

  <ImageButton

  android:id="@+id/ImageButton01"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:background="@drawable/a">

  </ImageButton>

  <ImageButton

  android:id="@+id/ImageButton02"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:background="@drawable/c">

  </ImageButton>

  </LinearLayout>

  再雙擊打開MainActivity.java文件,導(dǎo)入TextView控件類、ImageButton控件類聲明。

  import android.widget.TextView;

  import android.widget.ImageButton;

  在程序中加入文本控件和兩個ImageButton控件的引用代碼。

  final TextView textview1=(TextView)findViewById(R.id.TextView01);

  final ImageButton imagebutton1=(ImageButton)findViewById(R.id.ImageButton01);

  final ImageButton imagebutton2=(ImageButton)findViewById(R.id.ImageButton02);

  分別添加imagebutton1和imagebutton2的執(zhí)行代碼。點擊圖片按鈕會改變按鈕。

  imagebutton1.setOnClickListener(new OnClickListener(){

  @Override

  public void onClick(View v) {

  textview1.setText("點擊了第一個圖片按鈕");

  imagebutton1.setImageDrawable(getResources().getDrawable(R.drawable.b));

  imagebutton2.setImageDrawable(getResources().getDrawable(R.drawable.c));

  }

  });

  imagebutton2.setOnClickListener(new OnClickListener(){

  @Override

  public void onClick(View v) {

  textview1.setText("點擊了第二個圖片按鈕");

  imagebutton2.setImageDrawable(getResources().getDrawable(R.drawable.d));

  imagebutton1.setImageDrawable(getResources().getDrawable(R.drawable.a));

  }

  });

  運行程序,分別點擊ImageButton1和ImageButton2,可見如下效果:

  END

看了“android5.0程序開發(fā)的圖片按鈕ImageButton使用教程”的人還看了

1.android基礎(chǔ)教程視頻:Button圖文混排的按鈕

2.android中透明按鈕的設(shè)置教程

3.android基礎(chǔ)教程視頻:ToggleButton按鈕的使用

4.android基礎(chǔ)教程視頻:ImageView實現(xiàn)適屏和裁剪圖片

2076832