目录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开发有了大概的认识,而且有了基础的开发的能力,有点手痒痒想重拾团队项目了,看看时间吧。。