Android精通教程V

来源:https://www.cnblogs.com/dashucoding/archive/2018/07/04/9264894.html
-Advertisement-
Play Games

前言 大家好,給大家帶來 的概述,希望你們喜歡 前言 如果你想學習Android開發,那你就要瞭解Java編程,這是基礎,也是重點,如果沒學Java語法就先學習,再來學Android,別問可不可以先學Android,都告訴了,先學Java對吧! Android開發的基本瞭解 Android開發主要了 ...


前言

大家好,給大家帶來Android精通教程V的概述,希望你們喜歡
封面

前言

如果你想學習Android開發,那你就要瞭解Java編程,這是基礎,也是重點,如果沒學Java語法就先學習,再來學Android,別問可不可以先學Android,都告訴了,先學Java對吧!

Android開發的基本瞭解

Android開發主要瞭解這四種重要組件:

  • activity為用戶界面,就是說activity可以構成用戶界面。
  • ContentProvider是為了設備中存儲的數據,通過創建ContentProvider來實現數據共用。
  • Service是運行在後臺的任務,無需用戶直接與之交互。
  • Intent是一種行為描述機制(如選擇照片,打電話等)。在Android中,幾乎一切都是通過Intent來實現的,這給我們提供了大量替換或重用組件的機會。

    描述Android項目結構

    AndroidManifest.xml:是一個xml文件,描述了被構建的應用程式。
    assets:文件夾是為了存放需要打包到應用程式的靜態文件。
    bin:文件夾是為了存放編譯過後的應用程式。
    gen:文件夾為了存放生成的源代碼。
    libs:文件夾是存放第三方包的jar文件。
    src:文件夾是程式的Java源代碼。
    res:文件夾存放的是應用程式的資源。
    在res文件夾中:
    res/drawable/:存放的是圖像
    res/layout/:存放是基於xml的文件。
    res/menu/:存放的是基於xml的菜單文件。
    res/raw/:存放的是通用的文件。
    res/valuse/:存放的是字元串。
    res/xml/:是通用的xml的文件。
    在bin文件夾中:
    bin/classes/:存放的是編譯後的Java類文件。
    在AndroidManifest.xml文件中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.gdmec.android.androidstudiodemo">
    <!--原為android:theme="@style/AppTheme"-->
    <!--去除ActionBar標題欄-->
    <!--添加應用圖標,app_icon-->
    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--添加實現類-->
        <activity android:name=".######"></activity>
    </application>
</manifest>

瞭解一下build.gradle(app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "cn.edu.gdmec.android.androidstudiodemo"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

瞭解基本佈局的部件

TextView:瞭解android:typeface,android:textStyle,android:textColor.
EditText:編輯框android:autoText,android:capitalize,android:digitas,android:singleLine.
內邊距:android:paddingLeft,android:paddingRight,android:paddingTop,android:paddingBottom
RelativeLayout佈局:android:layout_alignParentTop,android:layout_alignParentBottom,android:layout_alignParentLeft,android:layout_alignParentRight,android:layout_centerHorizontal,android:layout_centerVertical,android:centerHorizontal,android:layout_centerInParent.
android:layout_above,android:layout_below,android:layout_toLeftOf,android:layout_toRightOf,android:layout_alignTop, android:layout_alignBottom,android:layout_alignLeft,android:layout_alignRight,android:layout_alignBaseline.
TableLayout佈局:
android:stretchColumns,android:shrinkColumns,android:collapseColumns.

//TableLayout
<TableLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:stretchColumns="1">
 <TableRow>
  <TextView
   android:text="姓名:"/>
  <EditText
   android:id="@+id/xm"
   android:layout_span="3"/>
 </TableRow>
 <View
   android:layout_height="2px"
   android:background="#000000"/>
 <TableRow>
  <Button
   android:id="@+id/xx"
   android:layout_column="2"
   android:text="消除"/>
  <Button
   android:id="@+id/submit"
   android:text="發送"/>
 </TableRow>
</TableLayout>

適配器

Sting[] items={"h","j","k","a","s","d","b"};
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
 android:id="@+id/selection"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"/>
<ListView
 android:id="@android:id/list"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:drawSelectiorOnTop="false"/>
</LinearLayout>
public class ListViewDemo extends ListActivity{
 TextView selection;
 String[] items={"a","b","c","d","e","f","g"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   setListAdapter(new ArrayAdapter<String>(this,android.R.layout_simple_list_item_1,items));
   selection= findViewById(R.id.selection);
}
 public void onListItemClick(ListView parent,View v,int position,long id){
  selection.setText(items[position]);
 }
}

網格

GridView:android:numColumns,android:verticalSpacing,android:horizontalSpacing,android:columnWidth,android:stretchMode.

//適配器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
<TextView
 android:id="@+id/selection"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"/>
<AutoCompleTextView
 android:id="@+id/auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:completionThreshold="3"/>
</LinearLayout>
public class AutoCompleteDemo extends Activity implements TextWatcher{
 TextView selection;
 AutoCompleteTextView auto;
 String[] items={"aaav","bbbv","cccv","dddv","eeev","fffv","gggv"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   selection=findViewById(R.id.selection);
   auto=findViewById(R.id.auto);
   auto.addTextChangedListener(this);
   auto.setAdapter(new ArrayAdapter<String>(this,android.R.layout_simple_item_1,items));
}
 public void onTextChanged(CharSequence s, int start, int before, int count){
  selection.setText(auto.getText());
}
}

Gallery:android:spacing,android:spinnerSelector,android:drawSelectorOnTop

//適配器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://echema.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
<ImageView
 android:id="@+id/icon"
 android:layout_width="20px"
 android:layout_height="wrap_content"
 android:paddingLeft="2px"
 android:paddingRight="2px"
 android:paddingTop="2px"
 android:src="@drawable/image"/>
<TextView
 android:id="@+id/label"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="20sp"/>
</LinearLayout>
public class Demo extends ListActivity{
 TextView selection;
 String[] items = { "aaaa","asdg","bsdes","slfl","wete","wetwd","sdfefs"};
@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   setListAdapter(new ArrayAdapter<String>(this, R.layout.simple,R.id.label,items));
   selection=findViewById(R.id.selection);
}
public void onListItemClick(ListView parent,View v,int position,long id){
 selection.setText(items[position]);
 }
}
//動態的列表
public class Demo extends ListActivity{
 TextView selection;
 String[] items = { "aaaa","asdg","bsdes","slfl","wete","wetwd","sdfefs"};
@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   setListAdapter(new IconAdapter());
   selection=findViewById(R.id.selection);
}
public void onListItemClick(ListView parent,View v,int position,long id){
 selection.setText(items[position]);
 }
class IconAdapter extends ArrayAdapter {
 IconAdapter(){
  super(Demo.this,R.layout.simple,items);
 }
 public View getView(int position, View convertView, ViewGrop parent){
 LayoutInflater inflater=getLayoutInflater();
 View simple = inflater.inflate(R.layout.simple,parent,false);
 TextView label=simple.findViewById(R.id.simple);
 label.setText(items[position]);
 ImageView icon = simple.findViewById(R.id.icon);
 icon.setImageResource(R.drawable.icon);
}

日期與時間

DatePickerDatePickerDialog->DatePickerDialog-->OnDateChangedListenerOnDateSetListener
TimePickerTimePickerDialog->TimePickerDialog-->OnTimeChangedListenerOnTimeSetListener
主要示例代碼:

Calendar dateTime = Calendar.getInstance();
//日期
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener(){
 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
  dateTime.set(Calendar.YEAR,year);
  dateTime.set(Calendar.MONTH,monthOfYear);
  dateTime.set(Calendar.DAY_OF_MONTH,dayOfMonth);
  updateLabel();
  }
};
//時間
TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener(){
 public void onTimeSet(TimePicker view, int hourOfDay, int minute){
  dateTime.set(Calendar.HOUR_OF_DAY,hourOfDay);
  dateTime.set(Calender.MINUTE,minute);
  updateLabel();
  }
};
//日期的點擊按鈕
Button btn=findViewById(R.id.date);
btn.setOnClickListener(new View.OnClickListener(){
 public void onClick(View v){
  new DatePickerDialog(Demo.this,d,dateTime.get(Calendar.YEAR),dateTime.get(Calendar.MONTH),dateTime.get(Calendar.DAY_OF_MONTH)).show();
 }
});
//時間的點擊按鈕
Button btn=findViewById(R.id.time);
btn.setOnClickListener(new View.OnClickListener(){
 public void onClick(View v){
  new TimePickerDialog(Demo.this,t,dateTime.get(Calendar.HOUR_OF_DAY),dateTime.get(Calendar.MINUTE),true).show();
 }
});
//顯示Calendar
dateTimetv.getTime();// dtl.format();

創建時鐘

DigitalClockAnalogClock

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <AnalogClock 
 android:id="@+id/analogclock"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_alginParentTop="true"/>
 <DigitalClock
 android:id="@+id/digitalclock"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_below="@id/analogclock"/>
</RelativeLayout>

進度條(android.widget.ProgressBar)

進度條可以是水平的,也可以是旋轉輪,你可以用incrementProgressBy()來增加進度,也可以用setProgress()來增加進度。

進度條有很多樣式,Android提供了這些:

  • Widget.ProgressBar.Horizontal
  • Widget.ProgressBar.Small
  • Widget.ProgressBar.Large
  • Widget.ProgressBar.Inverse
    等。

android.widget.SeekBar
這個是ProgressBar的擴展,這個是可以滑動選擇的進度形式。

用戶可以通過 SeekBar.OnSeekBarChangeListener來操作滑塊的位置。

適配器-android.widget.Adapter

它的子類有:arrayadapter,baseadpter,cursoradapter,listadapter,simpleadapter,spinneradapter

WebView

android.webkit.WebView
這裡的WebView是顯示網頁的視圖,當我們要在WebView中載入網頁的時候,,我們要在android manifest.xml中添加許可權。

<uses-permission android:name = “android.permission.INTERNET” />  

如何用代碼,以下顯示:

 Uri uri = Uri.parse(url);
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);

顯示

WebView webview = new WebView(this);
 setContentView(webview);

進行載入:

webview.loadUrl(url);

顯示框

public void onClick(View view){
 if(view==button1){
  new AlertDialog.Builder(this).setTitle("Message").setMessage("ok").setNeutralButton("Close", new DialogInterface.OnClickListener(){
   public void onClick(DialogInterface dialog, int in){
   }
  }).show();
}

總結

  • 本文講了Android精通教程V,如果您還有更好地理解,歡迎溝通
  • 定位:分享 Android&Java知識點,有興趣可以繼續關註

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 版權聲明:未經博主允許不得轉載 補充 補充上一節,使用 是用來顯示列表項的,使用 需要兩個xml文件,一個是列表佈局,一個是單個列表項的佈局。如我們要在要顯示系統所有app列表項時,需要左邊app 視圖和右邊文本視圖。 一個是列表佈局 all_app_list.xml 單個列表項的佈局 list_i ...
  • 前言 大家好,給大家帶來 的概述,希望你們喜歡 學習目標 掌握兩個插件的安裝和使用,能夠實現代碼生成功能。 Android Code Generator是一款代碼生成的插件,幫助提高app的開發速度,只要打好佈局XML文件,就能幫你把Activity/Fragment/Adapter文件生成好。 而 ...
  • 平臺目的:該平臺主要用於室外,通過平板或者手機接收GPS坐標,實時繪製點、線、面數據,以便後續進行海域監測、土地確權、地圖測量提供有效數據和依據。 技術路線:Android studio3.0.1+Arcgis for android 100.2.1+GPS ...
  • if (textField == _phoneTF) { //支持刪除 if (range.length == 1 && string.length == 0) { return YES; } else if (_phoneTF.text.length ... ...
  • 1> 索引 雖然索引的目的在於提高資料庫的性能,但這裡有幾個情況需要避免使用索引。使用索引時,應重新考慮下列準則: 2> 事務 對於事務,就是資料庫的一次原子性的執行操作。原子性的執行操作為數據的整體性執行帶來的可靠安全性。在SQLite中,如果我們預設事務(會為每個插入和更新都創建一次事務,並且在 ...
  • 1.使用系統為我們提供了幾個抽象的標簽 ①include:重用 ②viewstub:按需載入 註意: ③merge:減少佈局層數 ...
  • 最近有小伙伴說,7.0適配整了一波,現在又要來適配8.0,真是一波未平一波又起 但是作為開發者來說,學無止境,不跟上時代的步伐,肯定會被時代所淘汰... 話說Android P已經在路上了,你準備好了嗎? 適配屬性 1、通知渠道(Channeld) 當然,適配8.0的第一步自然是把targeSdk升 ...
  • 1.首先下載Eclipse for android,點擊進入。下載這個版本可以省去ADT配置() 2.下載符合你電腦的版本 2.現在Android SDK,地址:http://tools.android-studio.org/index.php/sdk 3.啟動Eclipse,選擇windows>p ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...