Android控件複習:按鈕(Button)

主要內容:

1.按鈕監聽和長按按鈕監聽

2.“.9圖片文件的使用”

3.自定義點擊按鈕效果

-圖片模式

-顏色模式

實代碼例:

1.部分代碼

先來看佈局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
	<Button 
	    android:id="@+id/bt1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="Button"
	    android:textSize="25px"/>
	<TextView 
	    android:id="@+id/tv1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:textSize="30px"/>
	<Button 
	   android:layout_width="90dip"
   	   android:layout_height="45dip"
   	   android:layout_marginTop="1dip"
	   android:textSize="30px"
	   android:background="@drawable/image_btn_bird"
	   android:layout_gravity="center"/>
	<Button 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="button"
	   	android:background="@drawable/button_font"/>
	<Button 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:background="@drawable/send_button_off"/>
	<Button 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:background="@drawable/new1"/>
	<Button 
	    android:id="@+id/btTest"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="test"
	    android:onClick="selfDestruct"/>
	
	
</LinearLayout>

監聽code

 //普通監聽
        button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "點擊了button", Toast.LENGTH_LONG).show();
			}
		});
        
        //長監聽
        button1.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "長按button", Toast.LENGTH_LONG).show();
				return true;
			}
		});

2.      .9格式圖片在開發中應用的非常多,我們不能保證每部手機都能顯示標準,.9文件的圖片可以根據屏幕來自動的伸縮,保證界面友好

可以看出在這個按鈕中,我直接使用了new1.9.png的圖片格式,黑線部分就是自己處理的,處理方法


<Button 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:background="@drawable/new1"/>
但是這個點擊是沒有效果的,只是孤零零的一張圖片。我們下面加上判斷來切換不同的圖片顯示按鈕效果;

3.自定義效果

圖片模式:code 使用true和false來判斷

<?xml version="1.0" encoding="utf-8"?>   
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
     <item android:state_pressed="true" android:drawable="@drawable/send_button_on" />  
     <item android:state_pressed="false" android:drawable="@drawable/send_button_off" />   
</selector> 
顏色模式:code 和圖片時一類的
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="false">
    	    <shape>
    	        <solid android:color="#ff9d77"/> 
			    <stroke android:width="2dp" android:color="#dcdcdc" />
			    <padding  android:left="30dp"  android:top="10dp"  
        					android:right="30dp"   android:bottom="10dp" />    
    	    </shape>
    </item>
    
     <item android:state_pressed="true">
    	    <shape>
    	         <solid android:color="#ff00ff"/> 
    	          <stroke android:width="2dp" android:color="#ff00ff" />
    	          <corners android:radius="2dp" /> 
    	    </shape>
    </item>
     
</selector>


我們可以在應用中反覆調用我們的文件

源代碼


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章