您的位置:首页 > 移动开发

android studio for android learning (三)我的第一个APP

2016-05-18 22:30 501 查看

1.我的第一个APP,android studio版本是最新的2.1,通过一个activity来调用另一个activity,并显示,如果你也一起做一遍你将会学到…..

响应发送按钮

建立一个intent对象

创建另一个activity

接收消息并显示

1.1 创建我的第一个项目,在android studio的welcome界面上,点击new project. application name use MyFristApp,and company domain is N609,具体设置如下图中所示.









2.对项目中的关键文件进行简单的介绍,首先选择查看project,然后你就可以看到下面这个工程目录。



下面对这个工程中关键文件进行介绍

2.1 app/src/main/res/layout/activity_my.xml

这个文件是你建立android文件时新建的文件xml布局文件,包括文本,UI等还包含一些行为按钮。

2.2 app/src/main/java/com.n609.myfirstapp/MyActivity.java

主要的控制文件

2.3 app/src/main/AndroidManifest.xml

整个APP的基础,包含常用组件的声明

2.4 app/build.gradle

编译器配置的相关文件,里面包含一些基本的设置。

3.Running Your APP (on a Real Device huawei 5X)

3.1 将你的手机设置在开发者模式下,然后我们上面所建立的工程就能运行在上面了,在android studio(AS)中RUN,则上面的工程就可以在你手机上运行了(当然你也可以用模拟器,由于比较占内存,不建议使用,最好是用真机来测试)。

建立一个xml文件,这个文件上包含一个text filed和一个button,这此之前,需要先理解View 和 ViewGroup对象,ViewGroup是不可见的,View就是指那单个的对象,如UI,button,等,ViewGroup就像是一个容器,它们之间的关系可以通过下面的图来显示。



3.2 创建一个线性布局

在res/layout目录下创建一个content_my.xml文件。它包含相对布局和一个TextView。



content_my.xml文件修改成如下(注意这并不是最终的文件内容)

<LinearLayout 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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_my">


在上面代码中LinearLayout是一个显示组,是ViewGroup的一个子类,LinearLayout是 content_my.xml的一个元素。

下面关于LinearLayout进行展开,在android的布局中有大布局,分别是LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局),今天我们只涉及LinearLayout布局。



3.2在content_my.xml文件中添加另一个元素EditText,如下所示

<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />


android:hint:默认显示文件。

在显示的res/values目录中打开strings.xml文件。增加如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
</resources>


4. 增加一个Button

在上面的res/layout content_my.xml文件中增加一个按钮元素,增加完后的文件如下

<LinearLayout 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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_my">
<EditText android:id="@+id/edit_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>


上面的配置的具体文件含义不再解释,显示如下:



content_my.xml最终文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@activity_my">

<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>

</LinearLayout>


这时你可以运行你的APP,来看下效果,但点击无反应,不要着急,接下来会介绍如何来交互。



5.activity的交互

5.1首先给button元素增加onClick属性,修改res/layout/content_my.xml,如下所示

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />


android:onClick是属性值,sendMessage是activity的一个方法,当用户点击按钮时,系统就会调用这个方法。

在java/com.n609.myfristapp目录中打开MyActivity.java文件,增加一个sendMessage的方法,如下所示

public void sendMessage(View view) {
// Do something in response to button
}


以下不能少

是public

有一个void返回值

有一个view作为唯一的参数(这将是被点击的view)

如何完成sendMessage

在MyActivity.java中添加一个intent

public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
}


这时你会发现有一个错误,因为我们还没创建Display….这个类,先不用理,稍后我们会创建,这个构造函数有两个参数.

一个上下文作为第一个参数(这是因为Activity类是语境的一个子类)

该应用组件到该系统应提供的意图的等级(在此情况下,应启动的活性)

这时你需要import intent类,在AS中直接alt+enter IDE会自动的导入类。

5.2 为了获取到EditText元素,我们用findViewById()这个方法。

java/com.n609.myfristapp/MyActivity.java

public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
}


在这个文件顶部需要导入EditText类,用putExtra()方法来将文本值给消息对象intent

java/com.n609.myfristapp/MyActivity.java

public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}


在这个文件里继续添加

public class MyActivity extends AppCompatActivity {

public final static String EXTRA_MESSAGE = "com.n609.myfristapp.MESSAGE";
}


5.3启动一个activity传递消息对象intent,完成的sendMessage如下所示。

public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}


5.4 创建 第二个activity:DispalyMessageActivity,所有的activity子类需要实现onCreate()方法,这个方法是用来接收intent 的消息和渲染这个消息用的,在onCreate()方法中用setContentView()方法来定义一个布局文件。

在java/com.n609.myfristapp下建立一个新的activity,activityName is DisplayMessageActivity

右键:New > Activity > Blank Activity.

Activity Name: DisplayMessageActivity

Layout Name: activity_display_message

Title: My Message

Hierarchical Parent: com.mycompany.myfirstapp.MyActivity

Package name: com.mycompany.myfirstapp

打开 建立的DisplayMessageActivity.java,里面有 onCreate()方法需要实现,你需要实现这个方法。

5.5 修改AndroidManifest.xml,增加activity元素,如下所示

<application ... >
...
<activity
android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.n609.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.n609.myfirstapp.MyActivity" />
</activity>
</application>


6.接收消息对象,编辑 DisplayMessageActivity.java文件,最后文件如下所示。

package com.n609.myfristapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

//显示信息
public class DisplayMessageActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

// Set the text view as the activity layout
setContentView(textView);
}
}


其他完整文件如下,整个构成了一个APP,可以输入数据,通过点击按钮在下一个activity中显示。到此这个简单的APP算开发完成。

MyActivity.java

package com.n609.myfristapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class MyActivity extends AppCompatActivity {

public final static String EXTRA_MESSAGE = "com.n609.myfristapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_my);
}
//called when the user clicks the send button
public void sendMessage(View view){
//TODO
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText =(EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}


content_my.xml

package com.n609.myfristapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class MyActivity extends AppCompatActivity {

public final static String EXTRA_MESSAGE = "com.n609.myfristapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_my);
}
//called when the user clicks the send button
public void sendMessage(View view){
//TODO
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText =(EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}


activity_display_message.xml

package com.n609.myfristapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class MyActivity extends AppCompatActivity {

public final static String EXTRA_MESSAGE = "com.n609.myfristapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_my);
}
//called when the user clicks the send button
public void sendMessage(View view){
//TODO
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText =(EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}


activity_my.xml

<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.n609.myfristapp.MyActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello dragon" />
</RelativeLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.n609.myfristapp">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.n609.myfristapp.MyActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.n609.myfirstapp.MyActivity"/>
</activity>
</application>

</manifest>


效果图







reference:

https://developer.android.com/training/basics/firstapp/building-ui.html

/article/1370513.html

http://www.runoob.com/w3cnote/android-tutorial-linearlayout.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: