Android基礎(chǔ)控件(EditView、SeekBar等)的使用方法
android提供了大量的UI控件,本文將介紹TextView、ImageView、Button、EditView、ProgressBar、SeekBar、ScrollView、WebView的使用方法。在介紹各種控件之前,先簡(jiǎn)單介紹android UI控件最基本的幾種屬性:
id: id是控件唯一標(biāo)識(shí)符,可通過(guò)**findViewById(R.id.*)**操作控件。
layout_width:控件寬度,可設(shè)置為match_parent(充滿父布局,即讓父布局決定當(dāng)前控件的寬度)、wrap_content(恰好包住里面的內(nèi)容)、具體值(一般以dp作為單位)。
layout_width:控件高度,可設(shè)置為match_parent、wrap_content、具體值。
visibility:可見(jiàn)與否,有三個(gè)可選值:visible(可見(jiàn),不設(shè)置該屬性為默認(rèn)值)、invisible(透明,仍在屏幕上占據(jù)空間)、gone(不可見(jiàn),不占據(jù)空間)。
1.TextView(文本)
TextView可以說(shuō)是最簡(jiǎn)單的控件了。
1.1 基本屬性
<!--res/layout/activity_main.xml--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="30sp" android:textColor="#334433" android:text="@string/app_name" /> </LinearLayout>
text: text屬性即顯示出來(lái)的文字,@string/app_name表示引用資源文件res/values/strings.xml中的app_name,也可以直接寫(xiě)內(nèi)容。
<!--res/values/strings.xml--> <resources> <string name="app_name">UIExample</string> <string name="title_activity_main">MainActivity</string> </resources>
gravity: 對(duì)TextView中內(nèi)容位置的設(shè)定,可選值為top、bottom、left、right、center等??梢詾槎鄠€(gè)值,例如希望文字位于該TextView的右下角,設(shè)置為gravity="right|bottom",用 | 隔開(kāi)
textSize與textColor 文字大小與顏色。
id: 這里用了"\@+id/text",表示給id分配一個(gè)唯一標(biāo)識(shí)符text,與引用類似,多了一個(gè)+。
1.2 定義style
假如上面的樣式是標(biāo)題的樣式,且被重復(fù)使用多次。如果每個(gè)標(biāo)題都這樣定義樣式,不但增加工作量,而且會(huì)使用修改變得很困難,這種情況下,將style抽象出來(lái)能解決這個(gè)問(wèn)題。
這與Web開(kāi)發(fā)中CSS的作用如出一轍。
<!--res/values/styles.xml 增加TextTitle--> <resources> ... <style name="TextTitle"> <item name="android:textColor">#334433</item> <item name="android:textSize">30sp</item> <item name="android:gravity">center</item> </style> </resources> <!--res/layout/activity_main.xml 將TextView作如下修改即可--> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_name" style="@style/TextTitle" />
1.3 動(dòng)態(tài)操作
// 使用代碼動(dòng)態(tài)設(shè)置TextView中的文本內(nèi)容 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 通過(guò)findViewById,獲取TextView的實(shí)例。 // 使用setText()與getText()賦值和取值。 TextView textView = (TextView) findViewById(R.id.text); textView.setText("Hello World!"); Log.d("MainActivity",textView.getText().toString()); } }
2.ImageView(圖片)
有文本,自然少不了圖片。
<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/test_image" />
id: 若不在java代碼中動(dòng)態(tài)操作,id屬性可以省略。
src: 即定義顯示的圖片,將需要加載的圖片放置在 res/drawable/目錄下即可。
// 調(diào)用setImageResource()方法即可。 // 需要加載的圖片復(fù)制到 res/drawable/ 目錄下。 ImageView imageView = (ImageView) findViewById(R.id.image); imageView.setImageResource(R.drawable.test_image2);
3.Button(按鈕)
3.1 基本樣式
<Button android:id="@+id/button_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="I Am A Button" />
text: text即按鈕上的提示內(nèi)容
3.2 點(diǎn)擊事件
3.2.1 為點(diǎn)擊事件注冊(cè)監(jiān)聽(tīng)器
// 第12行,注冊(cè)監(jiān)聽(tīng)器OnClickListener,復(fù)寫(xiě)onClick()函數(shù)。 public class MainActivity extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text); Button button = (Button) findViewById(R.id.button_1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ... 省略點(diǎn)擊事件 // 例如:textView.setText("Hello World"); } }); } }
3.2.2 實(shí)現(xiàn)接口OnClickListener
// 第14行,將點(diǎn)擊事件綁定到 this // 第17行,復(fù)寫(xiě)接口OnClickListener的onClick()方法 // java中只能繼承一個(gè)類,接口可以看作java的多繼承方式 public class MainActivity extends Activity implements View.OnClickListener{ private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text); Button button = (Button) findViewById(R.id.button_1); button.setOnClickListener(this); } @Override public void onClick(View view){ // 根據(jù)id判斷,若一個(gè)活動(dòng)中有多個(gè)控件需綁定點(diǎn)擊事件 // 使用該方式,該方法簡(jiǎn)潔直觀 switch (view.getId()){ case R.id.button_1: // ... 省略點(diǎn)擊事件 // 例如:textView.setText("Hello World!"); break; default: break; } } }
4.EditText(輸入框)
4.1 基本樣式
<EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="3" android:hint="Please Input..." />
maxLines: 指定輸入框的最大行號(hào)為3行,超過(guò)3行后,文本將向上滾動(dòng),EditText不會(huì)繼續(xù)被拉伸。如果不指定,EditText將隨著輸入內(nèi)容的增加而被拉伸。
hint: 類似Html中的placeholder,用于輸入框的提示。
4.2 獲取輸入內(nèi)容
// 點(diǎn)擊按鈕,打印輸入的內(nèi)容。 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // ... Button button = (Button) findViewById(R.id.button_1); final EditText editText = (EditText) findViewById(R.id.edit); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 使用 getText()方法獲取editText的內(nèi)容 String input_text = editText.getText().toString(); Log.d("MainActivity",input_text); } }); } }
5.ProgressBar(進(jìn)度條)
5.1 圓形進(jìn)度條
<ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" />
visibility: visible(可見(jiàn))、invisible(透明,占據(jù)空間)、gone(不可見(jiàn),不占據(jù)空間),初始值設(shè)置為不可見(jiàn)。
// 一般比較耗時(shí)的工作,會(huì)暫時(shí)顯示進(jìn)度條,工作完成后,進(jìn)度條消失 // 以下代碼模擬該過(guò)程 public class MainActivity extends Activity implements View.OnClickListener{ private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { // ... Button button = (Button) findViewById(R.id.button_1); progressBar = (ProgressBar) findViewById(R.id.progress_bar); button.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button_1: // 點(diǎn)擊按鈕后,如果當(dāng)前狀態(tài)可見(jiàn),則變?yōu)椴豢梢?jiàn) // 如不可見(jiàn),則變?yōu)榭梢?jiàn) if(progressBar.getVisibility() == View.GONE) { progressBar.setVisibility(View.VISIBLE); } else { progressBar.setVisibility(View.GONE); } break; default: break; } } }
5.2 水平進(jìn)度條
<ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" />
style: 設(shè)置style為水平樣式,其他樣式可以自己嘗試。
// 將onClick() 改為 @Override public void onClick(View view) { switch (view.getId()){ case R.id.button_1: // 獲取當(dāng)前進(jìn)度值,每次點(diǎn)擊進(jìn)度值+10 int progress = progressBar.getProgress(); progressBar.setProgress(progress + 10); default: break; } }
6.SeekBar(滑動(dòng)條)
6.1 基本樣式
<SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" /> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" />
max: 滑動(dòng)條的最大值,設(shè)置為 100
progress: 初始化滑動(dòng)條的值,設(shè)置為 50
6.2 注冊(cè)滑動(dòng)監(jiān)聽(tīng)器
public class MainActivity extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text); SeekBar seekBar = (SeekBar) findViewById(R.id.seek_bar); // 設(shè)置滑動(dòng)監(jiān)聽(tīng)器,復(fù)寫(xiě)三個(gè)事件函數(shù),分別是值改變、開(kāi)始滑動(dòng)、結(jié)束滑動(dòng) seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if(fromUser){ // 用戶主動(dòng)滑動(dòng),值改變時(shí)觸發(fā)該事件 textView.setText("用戶正在滑動(dòng),當(dāng)前值:" + progress); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { Log.d("MainActivity","開(kāi)始滑動(dòng)時(shí)觸發(fā)該事件"); } @Override public void onStopTrackingTouch(SeekBar seekBar) { Log.d("MainActivity","結(jié)束滑動(dòng)時(shí)觸發(fā)該事件"); } }); } }
7.SrollView(可滾動(dòng)視圖)
<ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <!-- ... 省略其他控件--> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView>
當(dāng)TextView的內(nèi)容過(guò)多時(shí)(一頁(yè)顯示不完),這時(shí)使用ScrollView可以使頁(yè)面變?yōu)榭纱怪睗L動(dòng)模式,垂直滾動(dòng)看完所有內(nèi)容。
8.WebView(瀏覽器)
<WebView android:id="@+id/web_view" android:layout_width="match_parent" android:layout_height="match_parent" />
在AndroidManifest.xml中加入訪問(wèn)網(wǎng)絡(luò)的權(quán)限
<manifest ... > <application ...> ... </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest>
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView) findViewById(R.id.web_view); // 允許執(zhí)行javasript webView.getSettings().setJavaScriptEnabled(true); // 設(shè)置代理,復(fù)寫(xiě)shouldOverrideUrlLoading函數(shù) webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view,String url){ view.loadUrl(url); //加載網(wǎng)頁(yè) return true; //true表示使用當(dāng)前WebView打開(kāi)網(wǎng)頁(yè),不使用系統(tǒng)瀏覽器 } }); webView.loadUrl("http://www.dbjr.com.cn"); } }
運(yùn)行程序,類似于一個(gè)打開(kāi)了網(wǎng)頁(yè)的瀏覽器,只是缺少網(wǎng)址輸入框。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Android自定義View實(shí)現(xiàn)五子棋游戲
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android中日期與時(shí)間設(shè)置控件用法實(shí)例
這篇文章主要介紹了Android中日期與時(shí)間設(shè)置控件用法,實(shí)例分析了Android日期與時(shí)間相關(guān)控件的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Android簡(jiǎn)單實(shí)現(xiàn)畫(huà)圖功能
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單實(shí)現(xiàn)畫(huà)圖功能的方法,以及實(shí)現(xiàn)過(guò)程中遇到的問(wèn)題,感興趣的小伙伴們可以參考一下2016-03-03android中NFC讀寫(xiě)功能的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了android中NFC讀寫(xiě)功能的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android實(shí)現(xiàn)登錄注冊(cè)功能封裝
Android應(yīng)用軟件基本上都會(huì)用到登錄注冊(cè)功能,本篇文章主要介紹了Android登錄注冊(cè)功能封裝功能實(shí)現(xiàn),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01Android編程實(shí)現(xiàn)讀取工程中的txt文件功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)讀取工程中的txt文件功能,結(jié)合實(shí)例形式詳細(xì)分析了Android讀取txt文件的原理、操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02利用Android實(shí)現(xiàn)比較炫酷的自定義View
自定義View、多線程、網(wǎng)絡(luò),被認(rèn)為是Android開(kāi)發(fā)者必須牢固掌握的最基礎(chǔ)的三大基本功,這篇文章主要給大家介紹了關(guān)于如何利用Android實(shí)現(xiàn)比較炫酷的自定義View的相關(guān)資料,需要的朋友可以參考下2021-07-07Android仿高德首頁(yè)三段式滑動(dòng)效果的示例代碼
很多app都會(huì)使用三段式滑動(dòng),比如說(shuō)高德的首頁(yè)和某寶等物流信息都是使用的三段式滑動(dòng)方式。本文將介紹如何實(shí)現(xiàn)這一效果,感興趣的可以學(xué)習(xí)一下2022-01-01