【Android開發-9】資訊時代,Activity和Activity怎麼交流

來源:互聯網
上載者:User

標籤:android   style   http   color   os   java   io   strong   for   

前言:在資訊時代,人們不必像古代人那樣飛鴿傳書,隨便一個簡訊、、QQ、微博都可以和自己親愛的小夥伴快速溝通交流。在這樣眼花繚亂的資訊時代,選擇一種合適自己的溝通工具是很有必要的,Android中的Activity與Activity之間的傳參方法也是很多種的,在項目中怎麼選擇資訊互動方法,就看項目的需求和自己的經驗。


弄例子之前需要瞭解一個東西:Intent;

書上和網上看到的概念解釋都很多,但本人喜歡它的英文意思:意圖。你的意圖想幹嘛,跟它說了就行。比如我要傳參,那你在這個Intent中設定完了,到第二個介面通過你設定的意圖傳參,就可以解析出來。比如你意圖要開啟介面,你在Intent裡面設定好要開啟的介面參數,然後通過方法調用就可以。或者更簡單的說明就是起傳遞作用!


開始實戰理解:

1.建立個項目:ActivityIntent


2.根據上一篇,創另外一個介面,建立的檔案為:activity_first.xml和FirstActivity


activity_first.xml中介面布局的代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"   >    <EditText        android:id="@+id/text"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text=""        android:layout_centerHorizontal="true" />        <Button         android:id="@+id/close_1"        android:layout_below="@id/text"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="關閉"                /></RelativeLayout>


主介面布局檔案代碼如下:

<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"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="介面之間傳參"        android:layout_centerHorizontal="true" />    <Button         android:id="@+id/Open_1"        android:layout_below="@id/title"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Intent傳參(第一種)"                />      <Button         android:id="@+id/Open_2"        android:layout_below="@id/Open_1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Intent傳參(第二種)"                />        <Button         android:id="@+id/Open_3"        android:layout_below="@id/Open_2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Intent傳參(第三種)"                /></RelativeLayout>


3.把建立的介面布局加入程式,修改下AndroidManifest.xml,在</application>節點前新增內容如下::

 <activity            android:name=".FirstActivity"            android:label="@string/app_name" > </activity>

4.編寫入口代碼MainActivity如下:

package com.wyz.activityintent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button intentBtn = (Button)findViewById(R.id.Open_1);Button intentBtn_2 = (Button)findViewById(R.id.Open_2);Button intentBtn_3 = (Button)findViewById(R.id.Open_3);intentBtn.setOnClickListener(new BtnListener());intentBtn_2.setOnClickListener(new BtnListener());intentBtn_3.setOnClickListener(new BtnListener());}class BtnListener implements OnClickListener {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, FirstActivity.class);switch(v.getId())              {              case R.id.Open_1:  //第一種                        intent.putExtra("name", "wyz_1_");            intent.putExtra("age", 18);            startActivity(intent);            break;                        case R.id.Open_2:  //第二種            Bundle bundle = new Bundle();            bundle.putString("name", "wyz_2_");            bundle.putInt("age", 27);                        intent.putExtras(bundle);            startActivity(intent);            break;            case R.id.Open_3: //第三種            intent.putExtra("name", "wyz_3_");            intent.putExtra("age", 35);                        startActivityForResult(intent, 0);                        break;            default:            break;            }}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if(requestCode == 0)//這個請求碼對應startActivityForResult(intent, 0);{if(resultCode == 1)  //這個請求碼對應開啟介面的setResult(1, resultIntent);{String sRet = data.getStringExtra("return");  Toast.makeText(getApplicationContext(), sRet, Toast.LENGTH_LONG).show();;         }}super.onActivityResult(requestCode, resultCode, data);}}


4.編寫另外一個介面業務代碼FirstActivity如下:

package com.wyz.activityintent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class FirstActivity extends Activity {int iType=0;//判斷是第幾種方法觸發,預設0@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);EditText text = (EditText)findViewById(R.id.text);Button close_btn = (Button)findViewById(R.id.close_1);close_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(iType == 3)//等於第三種時,關閉介面返回參數{Intent resultIntent = new Intent();  resultIntent.putExtra("return", "Hello wyz");  setResult(1, resultIntent);  FirstActivity.this.finish();  }else{FirstActivity.this.finish();}FirstActivity.this.finish();}});Intent intent = getIntent();String sName = intent.getStringExtra("name");//int iAge = intent.getIntExtra("age", 0);  //擷取參數第一種int iAge = intent.getExtras().getInt("age");//擷取參數第二種String sAge = String.valueOf(iAge);text.setText(sName+sAge);//設定顯示結果if(sName.compareTo("wyz_3_") == 0){iType = 3;}}}


實戰效果:








【Android開發-9】資訊時代,Activity和Activity怎麼交流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.