android學習四---Activity和Intent

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

1.android項目資源深入瞭解

      在深入學習android之前,先好好玩玩手機上的應用,大部分程式都有一個表徵圖,點開表徵圖,程式啟動,一定時間後,程式會跳轉到第一個介面,比如手機QQ,點開表徵圖,會跳出一幅圖,接著就跳到QQ登陸的介面(如)。這個介面裡有輸入框輸入QQ號碼,密碼,有個登陸按鈕,有記住密碼選擇框,還有一些表徵圖和文本。如果你輸入了密碼和帳號後,點擊登陸,程式就會響應。發送到伺服器端會檢查帳號,密碼。帳號密碼一致的話就會就會跳到使用者介面。

     假設需要實現這樣的一個介面,對應的android項目裡需要哪些資源呢。首先需要在res資源檔夾中的drawable裡放置一張初始顯示的圖片,value裡的string裡放置各種button上顯示的字串資源。然後登陸介面就是一個activity,這個activity上能夠對各種按鈕,文字框進行響應或者跳轉到另一個activity,這個監聽或者響應的代碼就在src的java原始碼裡定義。而這個介面的布局檔案在xml檔案裡。添加了一個資源,在gen裡的R.java會自動產生索引。

2.一個簡單的android項目2.1 效果

點擊activity1中的send會跳轉到Activity2,然後點擊back就會回到activity1。

2.2 構思

首先要構建兩個介面,每個介面是activity,我們定義介面一的activity為activity1,定義介面二的activity為Activity2.分別為介面一二建立布局。程式中主要是解決兩個問題,(1)如何知道按鍵被按下了(2)兩個activity之間如何進行通訊。

對於第一個問題,先要取得布局中的button對象,然後調用對象的setOnClickListener 進行監聽,當監聽的事件發生後(按下按鍵),就執行onClick方法。

對於第二個問題,需要採用android的一個類Intent來實現,Intent顧名思義意圖即程式想要表達的意圖,這個意圖怎麼表達的呢?需要實施的動作和具體的資料。這裡的Intent的作用主要是協助完成android各個組件之間的通訊。比如說調用startActivity()來啟動一個activity,或者由broadcaseIntent()來傳遞給所有感興趣的BroadcaseReceiver, 再或者由startService()/bindservice()來啟動一個背景service。

2.3 實現

先建立一個blank activity,在產生的.xml檔案裡定義一個button。

<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.example.activitiy1.MainActivity" >     <Button        android:id="@+id/buttonsend"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button_send"         android:onClick="sendMessage"/></RelativeLayout>

android:id="@+id/buttonsend" 代表Button的id ,第一次添加資源時需要加+號,名字可以隨意取,最好取有意義的好東岸。

android:layout_width:定義當前視圖在螢幕上所佔的寬度, fill_parent/ match_parent 即填充整個螢幕。
android:layout_height:定義當前視圖在螢幕上所佔的高度, fill_parent/ match_parent 即填充整個螢幕。

wrap_content:隨著文字大小的不同而改變這個視圖的寬度或高度。

android:text="@string/button_send":引用的是string裡的字串,這裡的字串都需要在values裡的String.xml中進行定義。

定義方式為<string name="button_send">send</string>。兩個<><>間的字串即為在介面中顯示的,支援中文。

同樣點擊new –other,建立一個Activity2

填寫Activity Name 和Layout Name,本項目中採用的名字分別是Activity2和main_activity2。

同樣建立介面main_activity2.xml代碼

<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.example.activitiy1.Activity2" >   <Button        android:id="@+id/buttonback"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button_back"         android:onClick="sendMessage"/></RelativeLayout>

介面都弄好之後,就需要在src原始碼裡實現相應功能。主要是在Oncreate的時候,監聽按鍵,按鍵按下時,建立一個Intent,然後利用這個Intent來啟動另一個activity。‘

MainActivity.java和Activity2的代碼如下

package com.example.activitiy1;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button button=(Button) findViewById(R.id.buttonsend);        button.setOnClickListener(new Button.OnClickListener(){            public void onClick(View v){                Intent intent =new Intent();                intent.setClass(MainActivity.this, Activity2.class);                startActivity(intent);                MainActivity.this.finish();                }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

 

 

 

 

Activity2

package com.example.activitiy1;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class Activity2 extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_activity2);        Button button=(Button) findViewById(R.id.buttonback);        button.setOnClickListener(new Button.OnClickListener(){            public void onClick(View v){                Intent intent =new Intent();                intent.setClass(Activity2.this, MainActivity.class);                startActivity(intent);                Activity2.this.finish();                }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.activity2, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.activitiy1"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".Activity2"            android:label="@string/title_activity_activity2" >        </activity>    </application></manifest>

android學習四---Activity和Intent

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.