Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android學習筆記之四

Android學習筆記之四

編輯:關於Android編程

基本視圖介紹

1.文本 按鈕與輸入框

文本 按鈕 輸入框的繼承關系

這裡寫圖片描述

TextView:
android:text=”文本”
android:textSize=”20sp”
android:textColor=”#FF0”
android:textStyle=”bold”
android:lines=”3”
android:singleLine=”true”
android:typeface=”monospace” //設置字型。字形有:normal, sans, serif,monospace
android:clickable=””

Button:
屬性與TextView基本相似。
不同點:
1. 按鈕是自帶了背景的控件
2. 按鈕是可以點擊了

EditText:
android:hint=”請輸入QQ”
android:editable=”true”
android:maxLength=”50” 設置最大的字數
android:inputType=”textPassword”

2.是非選擇框

ToggleButton:
android:textOn=”開啟”
android:textOff=”關閉”
android:checked=”true”

RadioButton(單選框):
RadioGroup 單選組
RadioButton 單選框
一般它們是配合使用的
設置監聽器的時候,通過 RadioGroup radioGroup.setOnCheckedChangeListener();
android:checked=”true”

CheckBox:
設置監聽器的時候 每個CheckBox都應該設置
android:checked=”false”

附 ToggleButton和CheckBox練習代碼:

MainActivity.java

package com.m520it.select;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity{
    private List mChooseData = new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CheckBox aCbx = (CheckBox) findViewById(R.id.cbx_apple);
        CheckBox bCbx = (CheckBox) findViewById(R.id.cbx_banance);
        CheckBox cCbx = (CheckBox) findViewById(R.id.cbx_orange);
        myCheckedChangeListener listener = new myCheckedChangeListener();
        aCbx.setOnCheckedChangeListener(listener);
        bCbx.setOnCheckedChangeListener(listener);
    }
    class myCheckedChangeListener implements OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            int CbxId = buttonView.getId();
            if(CbxId==R.id.cbx_apple){
                if(isChecked){
                    mChooseData.add("蘋果");
                }else{
                    if(mChooseData.contains("蘋果")){
                        mChooseData.remove("蘋果");
                    }
                }
            }else if(CbxId==R.id.cbx_banance){
                if(isChecked){
                    mChooseData.add("香蕉");
                }else{
                    if(mChooseData.contains("香蕉")){
                        mChooseData.remove("香蕉");
                    }
                }
            }else if(CbxId==R.id.cbx_orange){
                if(isChecked){
                    mChooseData.add("橘子");
                }else{
                    if(mChooseData.contains("橘子")){
                        mChooseData.remove("橘子");
                    }
                }
            }
        }
    }
    public void myClick(View v){
        String result = "";
        for (int i = 0; i < mChooseData.size(); i++) {
            result+=mChooseData.get(i);
        }
        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
    }
}

布局文件activity_main.xml:



    

    

    

    

附 RadioGroup和RadioButton練習代碼:

MainActivity.java

package com.m520it.select;
import android.app.Activity;
import android.os.Bundle;

import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCheckedChangeListener {

    private RadioButton mARg;
    private RadioButton mBRg;
    private RadioButton mCRg;

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

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (R.id.rg_a == checkedId) {
            Toast.makeText(this, mARg.getText().toString(), Toast.LENGTH_SHORT)
                    .show();
        } else if (R.id.rg_b == checkedId) {
            Toast.makeText(this, mBRg.getText().toString(), Toast.LENGTH_SHORT)
                    .show();
        } else if (R.id.rg_c == checkedId) {
            Toast.makeText(this, mCRg.getText().toString(), Toast.LENGTH_SHORT)
                    .show();
        }
    }

    public void radioEvent() {
        RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
        mARg = (RadioButton) findViewById(R.id.rg_a);
        mBRg = (RadioButton) findViewById(R.id.rg_b);
        mCRg = (RadioButton) findViewById(R.id.rg_c);
        rg.setOnCheckedChangeListener(this);
    }
}

布局文件R.layout.radiogroup:




    

    

    

3.進度條

沒有進度的進度條

大進度條
style=”?android:attr/progressBarStyleLarge” 普通進度條
style=”?android:attr/progressBarStyle” 小進度條
style=”?android:attr/progressBarStyleSmall”

有進度的進度條

style=”?android:attr/progressBarStyleHorizontal”
android:progress=”80”
android:max=”100”

3.可拖動的進度條

SeekBar:可拖動的進度條
android:max=”100”
android:progress=”50”

4.星星進度條

RatingBar:星星進度條 基本單位為半顆星
android:numStars=”4”
android:rating=”3.5”
android:stepSize=”0.5”

練習代碼:



    

    

    

    
    

    

    

    
    
    

3.圖片控件(ImageView)

設置圖片源
android:src=”@drawable/ic_launcher”

代碼設置圖片源
imageView.setImageResource(resId)
imageView.setImageBitmap(bm);
imageView.setImageDrawable(drawable);

設置縮放模式
android:scaleType=”“

這裡寫圖片描述

練習demo:

MainActivity.java

package com.m520it.imageView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView = (ImageView) findViewById(R.id.iv2);
        /*imageView.setImageResource(resId)
        imageView.setImageBitmap(bm);
        imageView.setImageDrawable(drawable);*/
        imageView.setImageResource(R.drawable.flower);
    }
}

布局文件activity_main.xml



    

    

4.滑動控件

手機界面無法容納更多的控件的時候,就需要滾動界面。 ScrollView :控制上下滑動的效果 HorizontalScrollView: 控制左右滑動的效果 ScrollView與HorizontalScrollView只能容納一個子控件。
HorizontalScrollView的布局demo


    

        

        

    




ScrollView的布局demo



    

         

        

        

    

5.日期時間選擇器

直接敲一個demo就理解了

MainActivity.java

package com.m520it.dateAndTime;

import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnDateChangedListener {

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

        //獲取日期控件
        DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker);
        Calendar calendar = Calendar.getInstance(Locale.CHINESE);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        //init用來初始化改變後的日期
        //this 日期改變的監聽器
        datePicker.init(year, month, day,this);

        //獲取時間控件
        TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
        //設置24小時
        timePicker.setIs24HourView(true);
        //設置監聽器
        timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                System.out.println(hourOfDay+"..."+minute);
                Toast.makeText(MainActivity.this,hourOfDay+"..."+minute,Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        Toast.makeText(this,"year:"+year+" mothOfYear:"+(monthOfYear+1)+" dayOfMonth:"+dayOfMonth, Toast.LENGTH_SHORT).show();
    }
}

布局文件activity_main.xml:



    
    

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved