目錄html
啓動組件
四種組件類型中的三種 — Activity、服務和廣播接收器 — 經過名爲 Intent 的異步消息進行啓動。Intent 會在運行時將各個組件相互綁定(您能夠將 Intent 視爲從其餘組件請求操做的信使),不管組件屬於您的應用仍是其餘應用。java
清單文件
在 Android 系統啓動應用組件以前,系統必須經過讀取應用的 AndroidManifest.xml 文件(「清單」文件)確認組件存在。 您的應用必須在此文件中聲明其全部組件,該文件必須位於應用項目目錄的根目錄中。android
Android Studio界面和Idea基本一致,左側是項目的文件結構目錄
git
咱們主要使用的文件在app目錄下android-studio
要輸出咱們本身的學號能夠直接去res->layout->activity_main.xml
修改。建立項目的時候,默認有一個居中的TextView組件,內容是helloworld,咱們增長本身的文本,結果以下app
onCreate()
在系統建立活動時觸發。必須在此處調用setContentView()
以定義活動用戶界面的佈局。onCreate()完成後,接下來回調的永遠是onStart()onStart()
onCreate()退出時,活動進入開始狀態onResume()
系統在活動開始與用戶交互以前調用此回調。應用程序的大多數核心功能都是在該onResume()
方法中實現的。onPause()
當活動失去焦點並進入暫停狀態時, 系統會調用。例如,當用戶點擊「後退」或「最近」按鈕時,會出現此狀''態。onStop()
當活動再也不對用戶可見時, 系統會調用。onRestart()
當處於「已中止」狀態的活動即將從新啓動時,系統將調用此回調onDestroy()
系統在銷燬活動以前調用此回調<manifest ... > <application ... > <activity android:name=".ExampleActivity" /> ... </application ... > ... </manifest >
public class MainActivity extends Activity implements View.OnTouchListener { @SuppressLint("ClickableViewAccessibility") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv1 = findViewById(R.id.textView1); tv1.setOnTouchListener(this); TextView tv2 = findViewById(R.id.textView2); tv2.setOnTouchListener(this); } @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouch(View arg0, MotionEvent event) { if (arg0.getId()==(R.id.textView1)){ Intent intent = new Intent(this, SecondActivity.class); startActivity(intent); } if (arg0.getId()==(R.id.textView2)){ Intent intent = new Intent(this, ThirdActivity.class); startActivity(intent); } return true; } }
TextView對象能夠調用setOnTouchListener
方法開啓監聽,當用戶按下該對象時觸發onTouch事件。用if語句判斷用戶觸發的是哪一個事件,跳轉到不一樣的頁面。異步
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Context context = getApplicationContext(); CharSequence text = "I am ... 20175211(響指)!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); } }
res/layout/filename.xml
<?xml version="1.0" encoding="utf-8"?> <ViewGroup xmlns:android="http://schemas.android.com/apk/res/android" android:id="@[+][package:]id/resource_name" android:layout_height=["dimension" | "match_parent" | "wrap_content"] android:layout_width=["dimension" | "match_parent" | "wrap_content"] [ViewGroup-specific attributes] > <View android:id="@[+][package:]id/resource_name" android:layout_height=["dimension" | "match_parent" | "wrap_content"] android:layout_width=["dimension" | "match_parent" | "wrap_content"] [View-specific attributes] > <requestFocus/> </View> <ViewGroup > <View /> </ViewGroup> <include layout="@layout/layout_resource"/> </ViewGroup>
ViewGroup
<View>
Value for android:id
對於ID值,一般應使用如下語法形式:"@+id/name"
,+
表示這是一個新的資源ID。aapt工具將在R.java
類中建立一個新的資源整數(若是它尚不存在)。例如
<TextView android:id="@+id/nameTextbox"/>
該nameTextbox名稱如今是附加到此元素的資源ID。而後,您能夠參考TextViewJava中與ID關聯的內容:
TextView textView = findViewById(R.id.nameTextbox);
此代碼返回TextView對象。ide
Value for android:layout_height and android:layout_width
高度和寬度值可使用Android支持的任何 維度單位(px,dp,sp,pt,in,mm)或如下關鍵字表示:
工具
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="2dp" android:paddingRight="2dp" tools:context=".MainActivity"> <LinearLayout android:id="@+id/choose" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:gravity="center|top" android:background="@android:color/white" android:orientation="horizontal"> <Button android:id="@+id/cancelButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="30dp" android:text="@string/cancel" android:layout_marginEnd="30dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/saveButton" android:layout_marginLeft="30dp" android:text="@string/save" android:layout_marginStart="30dp" /> </LinearLayout> <ImageView android:id="@+id/image" android:layout_width="150dp" android:layout_height="150dp" android:layout_marginTop="150dp" android:padding="4dp" android:layout_below="@+id/choose" android:layout_centerHorizontal="true" android:src="@android:drawable/ic_btn_speak_now" /> <LinearLayout android:id="@+id/filter_button_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/image" android:layout_marginTop="50dp" android:gravity="center" android:background="@android:color/white" android:orientation="horizontal"> <Button android:id="@+id/filerButton" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginRight="20dp" android:text="@string/filter"/> <Button android:id="@+id/shareButton" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="@string/share" /> <Button android:id="@+id/deleteButton" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:text="@string/delete" /> </LinearLayout> </RelativeLayout>
package cn.edu.besti.is.multicolorclock; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { int counter = 0; int [] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.GREEN, Color.LTGRAY, Color.MAGENTA, Color.RED, Color.WHITE, Color.YELLOW }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void changeColor(View view) { if (counter == colors.length) { counter = 0; } view.setBackgroundColor(colors[counter++]); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="90dp" android:onClick="changeColor"/> </RelativeLayout>
問題1:安裝中一系列問題
(1)一開始我打算將Android Studio安裝在虛擬機中,開啓AVD時報錯以下圖
解決:問題出在虛擬機沒有開啓虛擬化。AVD本質上也是一個虛擬機,只有宿主機開啓虛擬化技術才能夠建立虛擬機,因此在VMware更改設置
佈局
(2)接下來報錯以下
解決:問題出在當前用戶權限不夠,沒法使用/dev/kvm目錄,改權限就好了
(3)你覺得這樣就結束了嗎?不,還有問題。建立虛擬機須要大概8個G的空間,我在虛擬機上作哪裏給你找8個G?
解決(並無):更改配置文件,將建立的虛擬手機內存改成2G。可是改完之後,點擊運行,配置文件裏那個數字又變成了默認的大小。我一氣之下把配置文件改爲了只讀,Android Studio直接告訴我沒法寫入文件,報錯。。。你運行個AVD爲何要改配置文件啊?在baidu、google均無果後,我屈服了,在主機裏下了個Android Studio。然而此時距離提交還有24小時(* ̄︶ ̄)
問題2:書上講的也太簡要了吧。。就照着打,什麼意思都不知道,這作實驗有什麼意義。。在有限的篇幅裏想把什麼東西都講了,結果就是什麼都講很差。。
解決:老辦法,看文檔去吧,文檔真的什麼都有。。配合谷歌翻譯,味道好極了o( ̄▽ ̄)d
連接:適用於應用開發者的文檔
作實驗、學知識、寫代碼都還好,作出東西的成就感足以支撐我繼續肝下去,可是安裝軟件遇到問題帶來的挫敗感真的是。。。卡了我兩天,心情奇差。還好最後是安好了。經過此次試驗,我對Android開發有了大概的認識,而且有了基礎的開發的能力,有點手癢癢想重拾團隊項目了,看看時間吧。。