Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 中級開發 >> Android學習之Android控件(1)

Android學習之Android控件(1)

編輯:中級開發

介紹

在 android 中使用各種控件(VIEw)

ZoomControls - 放大/縮小按鈕控件

Include - 整合控件

VideoVIEw - 視頻播放控件

WebVIEw - 浏覽器控件

RatingBar - 評分控件

Tab - 選項卡控件

Spinner - 下拉框控件

Chronometer - 計時器控件

ScrollVIEw - 滾動條控件

1、ZoomControls 的 Demo

zoomcontrols.XML

vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>  
02.<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"  
03.    android:orIEntation="vertical" android:layout_width="fill_parent"  
04.    android:layout_height="fill_parent">  
05.    <!--  
06.        放大/縮小按鈕控件  
07.    -->  
08.    <ZoomControls android:id="@+id/zoomControls"  
09.        android:layout_width="wrap_content" android:layout_height="wrap_content"></ZoomControls>  
10.</LinearLayout>  
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
    android:orIEntation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!--
        放大/縮小按鈕控件
    -->
    <ZoomControls android:id="@+id/zoomControls"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ZoomControls>
</LinearLayout>

_ZoomControls.Java

vIEw plaincopy to clipboardprint?
01.package com.webabcd.vIEw;   
02.import android.app.Activity;   
03.import android.os.Bundle;   
04.import android.view.VIEw;   
05.import android.view.VIEw.OnClickListener;   
06.import android.widget.Toast;   
07.import android.widget.ZoomControls;   
08.public class _ZoomControls extends Activity {   
09.    @Override  
10.    protected void onCreate(Bundle savedInstanceState) {   
11.        //TODO Auto-generated method stub   
12.        super.onCreate(savedInstanceState);   
13.        this.setContentVIEw(R.layout.zoomcontrols);   
14.        setTitle("ZoomControls");   
15.        ZoomControls zoomControls=(ZoomControls) this.findVIEwById(R.id.zoomControls);   
16.        //setOnZoomInClickListener() - 響應單擊放大按鈕的事件   
17.        zoomControls.setOnZoomInClickListener(new OnClickListener() {   
18.            public void onClick(VIEw v) {   
19.                Toast.makeText(_ZoomControls.this, "單擊了放大按鈕", Toast.LENGTH_SHORT).show();   
20.            }   
21.        });   
22.        //setOnZoomOutClickListener() - 響應單擊縮小按鈕的事件   
23.        zoomControls.setOnZoomOutClickListener(new OnClickListener() {   
24.            public void onClick(VIEw v) {   
25.                Toast.makeText(_ZoomControls.this, "單擊了縮小按鈕", Toast.LENGTH_SHORT).show();       
26.            }   
27.        });   
28.    }   
29.}  
package com.webabcd.vIEw;
import android.app.Activity;
import android.os.Bundle;
import android.view.VIEw;
import android.view.VIEw.OnClickListener;
import android.widget.Toast;
import android.widget.ZoomControls;
public class _ZoomControls extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentVIEw(R.layout.zoomcontrols);
        setTitle("ZoomControls");
        ZoomControls zoomControls=(ZoomControls) this.findVIEwById(R.id.zoomControls);
        //setOnZoomInClickListener() - 響應單擊放大按鈕的事件
        zoomControls.setOnZoomInClickListener(new OnClickListener() {
            public void onClick(VIEw v) {
                Toast.makeText(_ZoomControls.this, "單擊了放大按鈕", Toast.LENGTH_SHORT).show();
            }
        });
        //setOnZoomOutClickListener() - 響應單擊縮小按鈕的事件
        zoomControls.setOnZoomOutClickListener(new OnClickListener() {
            public void onClick(VIEw v) {
                Toast.makeText(_ZoomControls.this, "單擊了縮小按鈕", Toast.LENGTH_SHORT).show();    
            }
        });
    }
}

2、Include的Demo

include.XML

vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>  
02.<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"  
03.    android:orIEntation="vertical" android:layout_width="fill_parent"  
04.    android:layout_height="fill_parent">  
05.    <!--   
06.        include - 整合控件,將指定的 layout 整合進來   
07.            layout - 指定需要整合 layout   
08.    -->  
09.    <include android:id="@+id/cell1" layout="@layout/include_1" />  
10.    <include android:id="@+id/cell2" android:layout_width="fill_parent" layout="@layout/include_2" />  
11.</LinearLayout>  
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
    android:orIEntation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!--
        include - 整合控件,將指定的 layout 整合進來
            layout - 指定需要整合 layout
    -->
    <include android:id="@+id/cell1" layout="@layout/include_1" />
    <include android:id="@+id/cell2" android:layout_width="fill_parent" layout="@layout/include_2" />
</LinearLayout>

include_1.XML

vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>  
02.<TextVIEw XMLns:android="http://schemas.android.com/apk/res/android"  
03.    android:text="TextVIEw01" android:layout_width="wrap_content"  
04.    android:layout_height="wrap_content">  
05.</TextVIEw>  
<?XML version="1.0" encoding="utf-8"?>
<TextVIEw XMLns:android="http://schemas.android.com/apk/res/android"
    android:text="TextVIEw01" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</TextVIEw>

include_2.XML

vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>  
02.<TextVIEw XMLns:android="http://schemas.android.com/apk/res/android"  
03.    android:text="TextVIEw02" android:layout_width="wrap_content"  
04.    android:layout_height="wrap_content">  
05.</TextVIEw>  
<?XML version="1.0" encoding="utf-8"?>
<TextVIEw XMLns:android="http://schemas.android.com/apk/res/android"
    android:text="TextVIEw02" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</TextVIEw>

_Include.Java

vIEw plaincopy to clipboardprint?
01.package com.webabcd.vIEw;   
02.import android.app.Activity;   
03.import android.os.Bundle;   
04.public class _Include extends Activity {   
05.    @Override  
06.    protected void onCreate(Bundle savedInstanceState) {   
07.        //TODO Auto-generated method stub   
08.        super.onCreate(savedInstanceState);   
09.        this.setContentVIEw(R.layout.include);   
10.        setTitle("Include");   
11.    }   
12.}  
package com.webabcd.vIEw;
import android.app.Activity;
import android.os.Bundle;
public class _Include extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentVIEw(R.layout.include);
        setTitle("Include");
    }
}

3、VideoVIEw的Demo

videovIEw.XML

vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>  
02.<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"  
03.    android:orIEntation="vertical" android:layout_width="fill_parent"  
04.    android:layout_height="fill_parent">  
05.    <!--  
06.        VideoVIEw - 視頻播放控件  
07.    -->  
08.    <VideoView android:id="@+id/videoVIEw" android:layout_width="wrap_content"  
09.        android:layout_height="wrap_content">  
10.    </VideoVIEw>  
11.</LinearLayout>  
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
    android:orIEntation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!--
        VideoVIEw - 視頻播放控件
    -->
    <VideoView android:id="@+id/videoVIEw" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </VideoVIEw>
</LinearLayout>

_VideoVIEw.Java

vIEw plaincopy to clipboardprint?
01.package com.webabcd.vIEw;   
02.import android.app.Activity;   
03.import android.Net.Uri;   
04.import android.os.Bundle;   
05.import android.widget.MediaController;   
06.import android.widget.VideoVIEw;   
07.public class _VideoVIEw extends Activity {   
08.    @Override  
09.    protected void onCreate(Bundle savedInstanceState) {   
10.        //TODO Auto-generated method stub   
11.        super.onCreate(savedInstanceState);   
12.        this.setContentView(R.layout.videovIEw);   
13.        setTitle("VideoVIEw");   
14.        VideoView videoView=(VideoView) findViewById(R.id.videoVIEw);   
15.        //指定需要播放的視頻的地址   
16.        videoVIEw.setVideoURI(Uri.parse("android.resource://com.webabcd.vIEw/" + R.raw.demo));   
17.        //videoVIEw.setVideoPath();   
18.        //設置播放器的控制條   
19.        videoVIEw.setMediaController(new MediaController(this));   
20.        //開始播放視頻   
21.        videoVIEw.start();   
22.    }   
23.}  
package com.webabcd.vIEw;
import android.app.Activity;
import android.Net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoVIEw;
public class _VideoVIEw extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.videovIEw);
        setTitle("VideoVIEw");
        VideoView videoView=(VideoView) findViewById(R.id.videoVIEw);
        //指定需要播放的視頻的地址
        videoVIEw.setVideoURI(Uri.parse("android.resource://com.webabcd.vIEw/" + R.raw.demo));
        //videoVIEw.setVideoPath();
        //設置播放器的控制條
        videoVIEw.setMediaController(new MediaController(this));
        //開始播放視頻
        videoVIEw.start();
    }
}

4、WebVIEw的Demo

webvIEw.XML

vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>  
02.<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"  
03.    android:orIEntation="vertical" android:layout_width="fill_parent"  
04.    android:layout_height="fill_parent">  
05.    <!--  
06.        WebVIEw - 浏覽器控件(WebKit 內核)  
07.    -->  
08.    <WebVIEw android:layout_width="fill_parent"  
09.        android:layout_height="wrap_content" android:id="@+id/webVIEw" />  
10.</LinearLayout>  
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
    android:orIEntation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!--
        WebVIEw - 浏覽器控件(WebKit 內核)
    -->
    <WebVIEw android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/webVIEw" />
</LinearLayout>

_WebVIEw.Java

vIEw plaincopy to clipboardprint?
01.package com.webabcd.vIEw;   
02.import android.app.Activity;   
03.import android.os.Bundle;   
04.import android.webkit.WebSettings;   
05.import android.webkit.WebVIEw;   
06.public class _WebVIEw extends Activity {   
07.    @Override  
08.    protected void onCreate(Bundle savedInstanceState) {   
09.        //TODO Auto-generated method stub   
10.        super.onCreate(savedInstanceState);   
11.        this.setContentView(R.layout.webvIEw);   
12.        setTitle("WebVIEw");   
13.        WebView webView=(WebView) findViewById(R.id.webVIEw);   
14.        //配置浏覽器,使其可支持 JavaScript   
15.        WebSettings webSettings=webVIEw.getSettings();   
16.        webSettings.setJavaScriptEnabled(true);   
17.        //清除浏覽器緩存   
18.        webVIEw.clearCache(true);   
19.        //指定浏覽器需要解析的 url 地址   
20.        webVIEw.loadUrl("http://webabcd.cnblogs.com/");  
21.        //指定浏覽器需要解析的 Html 數據   
22.        //webVIEw.loadData("<a href="http://webabcd.cnblogs.com/" mce_href="http://webabcd.cnblogs.com/">webabcd</a>", "text/Html", "utf-8");   
23.    }   
24.}  
package com.webabcd.vIEw;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebVIEw;
public class _WebVIEw extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.webvIEw);
        setTitle("WebVIEw");
        WebView webView=(WebView) findViewById(R.id.webVIEw);
        //配置浏覽器,使其可支持 JavaScript
        WebSettings webSettings=webVIEw.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //清除浏覽器緩存
        webVIEw.clearCache(true);
        //指定浏覽器需要解析的 url 地址
        webVIEw.loadUrl("http://webabcd.cnblogs.com/");
        //指定浏覽器需要解析的 Html 數據
        //webVIEw.loadData("<a href="http://webabcd.cnblogs.com/" mce_href="http://webabcd.cnblogs.com/">webabcd</a>", "text/Html", "utf-8");
    }
}

5、RatingBar的Demo

ratingbar.XML

vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>  
02.<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"  
03.    android:orIEntation="vertical" android:layout_width="fill_parent"  
04.    android:layout_height="fill_parent">  
05.    <!--   
06.        RatingBar - 評分控件   
07.            numStars - 評分控件的星星的數量   
08.            rating - 當前評分的值       
09.    -->  
10.    <RatingBar android:id="@+id/ratingBar" android:numStars="5"  
11.        android:rating="1.5" android:layout_width="wrap_content"  
12.        android:layout_height="wrap_content">  
13.    </RatingBar>  
14.    <TextView android:id="@+id/textVIEw" android:layout_width="wrap_content"  
15.        android:layout_height="wrap_content" />  
16.</LinearLayout>  
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
    android:orIEntation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!--
        RatingBar - 評分控件
            numStars - 評分控件的星星的數量
            rating - 當前評分的值    
    -->
    <RatingBar android:id="@+id/ratingBar" android:numStars="5"
        android:rating="1.5" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </RatingBar>
    <TextView android:id="@+id/textVIEw" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

_RatingBar.Java

vIEw plaincopy to clipboardprint?
01.package com.webabcd.vIEw;   
02.import android.app.Activity;   
03.import android.os.Bundle;   
04.import android.widget.RatingBar;   
05.import android.widget.TextVIEw;   
06.public class _RatingBar extends Activity implements RatingBar.OnRatingBarChangeListener {   
07.    private RatingBar mRatingBar;   
08.    private TextView mTextVIEw;   
09.    @Override  
10.    protected void onCreate(Bundle savedInstanceState) {   
11.        //TODO Auto-generated method stub   
12.        super.onCreate(savedInstanceState);   
13.        this.setContentVIEw(R.layout.ratingbar);   
14.        setTitle("RatingBar");   
15.        mTextView=(TextView) findViewById(R.id.textVIEw);   
16.        mRatingBar=(RatingBar) findVIEwById(R.id.ratingBar);   
17.        //setOnRatingBarChangeListener() - 響應評分值發生改變的事件   
18.        mRatingBar.setOnRatingBarChangeListener(this);   
19.    }   
20.    @Override  
21.    public void onRatingChanged(RatingBar ratingBar, float rating,   
22.            boolean fromUser) {   
23.        mTextVIEw.setText(String.valueOf(rating));   
24.    }   
25.} 

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