安卓開發(fā)(三)使用Intent

intent

MainActivity


package com.example.intentdemo;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivityextends AppCompatActivityimplements Button.OnClickListener{

Buttonsubmit;

? ? TextViewshow;

? ? int Sub_Activity =1;

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);

? ? ? ? submit = (Button) findViewById(R.id.submit);

? ? ? ? submit.setOnClickListener(this);

? ? ? ? show = (TextView) findViewById(R.id.show);

? ? }

@Override

? ? public void onClick(View v) {

Intent intent =new Intent(MainActivity.this, Main2Activity.class);

? ? ? ? startActivityForResult(intent, Sub_Activity);

? ? }

@Override

? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

? ? ? ? if (resultCode == Activity.RESULT_OK) {

Uri uriData = data.getData();

? ? ? ? ? ? show.setText("用戶名:" + uriData.toString());

? ? ? ? }

}

}

main.layout


? ? 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"

? ? android:rowCount="2"

? ? android:layout_column="1">

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="登錄"

? ? ? ? android:id="@+id/submit"

? ? ? ? android:layout_row="0"

? ? ? ? android:layout_column="0"

? ? ? ? android:layout_gravity="center"

? ? ? ? android:layout_marginTop="180dp"/>

? ? ? ? android:id="@+id/show"

? ? ? ? android:text="用戶名:"

? ? ? ? android:layout_row="1"

? ? ? ? android:layout_column="0"

? ? ? ? android:layout_marginTop="-190dp"

? ? ? ? android:layout_gravity="center"/>

SubActivity

package com.example.intentdemo;

import android.content.Intent;

import android.net.Uri;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class Main2Activityextends AppCompatActivityimplements Button.OnClickListener{

EditTextinput;

? ? Buttongoback;

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main2);

? ? ? ? input = (EditText) findViewById(R.id.input);

? ? ? ? goback = (Button) findViewById(R.id.goback);

? ? ? ? goback.setOnClickListener(this);

? ? }

@Override

? ? public void onClick(View v) {

Uri data = Uri.parse(input.getText().toString());

? ? ? ? Intent result =new Intent(null, data);

? ? ? ? setResult(RESULT_OK, result);

? ? ? ? finish();

? ? }

}

sub.layout

? ? 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=".Main2Activity"

? ? android:rowCount="2"

? ? android:columnCount="2">

? ? ? ? android:text="用戶名:"

? ? ? ? android:textSize="20dp"

? ? ? ? android:layout_row="0"

? ? ? ? android:layout_column="0"

? ? ? ? android:layout_marginTop="190dp"/>

? ? ? ? android:id="@+id/input"

? ? ? ? android:textSize="20dp"

? ? ? ? android:layout_width="280dp"

? ? ? ? android:layout_row="0"

? ? ? ? android:layout_column="1"

? ? ? ? android:layout_marginTop="190dp"/>

? ? ? ? android:id="@+id/goback"

? ? ? ? android:text="返回"

? ? ? ? android:layout_row="1"

? ? ? ? android:layout_columnSpan="2"

? ? ? ? android:layout_marginTop="40dp"

? ? ? ? android:layout_gravity="top|center" />

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容