Button ToggleButton Spinner Adapter Inflate

一、Button

1 SetImage

1.1 設置button背景圖片

 <Button
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="button"
       android:background="@drawable/background_bg"
       />

android:background=”@drawable/background1” //設置button的背景圖片

1.2 設置button的點擊效果(點擊時button背景圖切換)

res -- drawable -- 新建xml:background_bg.xml。

background_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="false" android:state_pressed="false"  android:drawable="@drawable/button1"/>
    <item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/button2"/>
    <item android:state_focused="true" android:state_pressed="false"   android:drawable="@drawable/button1"/>
    <item android:state_focused="true" android:state_pressed="true"   android:drawable="@drawable/button2"/>
    <item android:drawable="@drawable/button1"/>

</selector>

button的background屬性設置爲:

 android:background="@drawable/background_bg"

2 常用事件: onClick 和 onLongClick

設置button的onClick 和 onLongClick事件:

package com.example.chenjinhua.formwidgit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class FormWidgitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form_widgit);
        showButton();
    }
    public void showButton(){
        Button button1 = (Button) findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("jinhuatag","click button");
            }

        });

        button1.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Log.i("jinhuatag","button longclicked");
                return true;
            }
        });
    }

}

二、ToggleButton

1、 做漂亮的背景圖

2、增加事件 onCheckedChanged

package com.example.chenjinhua.formwidgit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

public class FormWidgitActivity extends AppCompatActivity {
    private ToggleButton toggleButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form_widgit);
        showToggleButton();

    }

    public void showToggleButton(){
         toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
         toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    toggleButton.setBackgroundResource(R.drawable.button1);
                    Toast.makeText(FormWidgitActivity.this, "開", 100).show();
                }else{
                    toggleButton.setBackgroundResource(R.drawable.button2);
                    Toast.makeText(FormWidgitActivity.this,"關",100).show();
                }
            }
        });
    }
}

注意:ToggleButton要設置BackGround的默認屬性:android:background=”@drawable/button2”
1)toggleButton.setBackgroundResource(R.drawable.button1); //設置狀態下背景圖片;
2)toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {} //點擊狀態

三、CheckBox

package com.example.chenjinhua.formwidgit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class FormWidgitActivity extends AppCompatActivity {
    private CheckBox checkBox;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form_widgit);
        showCheckBox();

    }

    public void showCheckBox() {
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        checkBox.setText("確定要鏈接wifi嗎?");
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(checkBox.isChecked()){
                    Toast.makeText(FormWidgitActivity.this,"選中",300).show();
                }
                if (!checkBox.isChecked()){
                    Toast.makeText(FormWidgitActivity.this,"未選中",300).show();
                }
            }
        });
    }
}

checkBox.setText(“確定要鏈接wifi嗎?”); //設置文案

四、RadioButton & RadioGroup

1、設置圖片
2、新增事件

package com.example.chenjinhua.formwidgit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;
import android.widget.Toast;

public class FormWidgitActivity extends AppCompatActivity {
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form_widgit);
        showRadioGroup();
    }

    public void showRadioGroup(){
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.radioButton1:
                        Toast.makeText(FormWidgitActivity.this,"radioButton1 clicked",100).show();
                    case R.id.radioButton2:
                        Toast.makeText(FormWidgitActivity.this,"radioButton2 clicked",100).show();
                    case R.id.radioButton3:
                        Toast.makeText(FormWidgitActivity.this,"radioButton3 clicked",100).show();
                        break;
                }
            }
        });
    }
}

3、去掉固有圖形或者改變位置

 <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/radioButton1"
            android:button="@null"
            android:drawableLeft="@drawable/girl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="yes"/>
        <RadioButton
            android:id="@+id/radioButton2"
            android:drawableRight="@android:drawable/btn_radio"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="no"/>
        <RadioButton
            android:id="@+id/radioButton3"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_star"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="both"/>
    </RadioGroup>

運行結果:

這裏寫圖片描述

1、android:button=”@null” //設置不顯示默認的左邊按鈕
2、android:drawableLeft=”@drawable/girl” //設置圖片
3、android:drawableRight=”@android:drawable/btn_star” //在右邊設置系統提供的圖標。

4、RadioGroup做工具條

五、Spinner
1、Adapter Pattern 及 Adapter

這裏寫圖片描述

所以界面顯示的步驟是:
1、建立數據源
2、建立adapter
3、建立adapter與數據源連接
4、綁定adapter到界面組件

2、android常用的Adapter

這裏寫圖片描述

3、Spinner常用操作:界面 和 事件

界面:

public class FormWidgitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form_widgit);
        showSpinner();
    }

    public void showSpinner(){
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        //建立數據源
        String[] myItemsString = getResources().getStringArray(R.array.spinnerName);
        //建立Adapter,連接數據源
        ArrayAdapter arrayAdapter = new ArrayAdapter(FormWidgitActivity.this,android.R.layout.simple_dropdown_item_1line,myItemsString);
        //綁定Adapter到UI組件
        spinner.setAdapter(arrayAdapter);
    }
}

事件:

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(FormWidgitActivity.this,((TextView)view).getText().toString(),100).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

4、擴展Adapter

發佈了53 篇原創文章 · 獲贊 13 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章