设计一个主Activity和一个子Activity(Sub-Activity),使用主Activity上的按钮启动子Activity,并将子Activity的一些信息返回给主Activity,并显示在主Activity上。
可以自己设计界面和场景,也可以使用下面提供的内容:
主Activity界面上有一个“登录”按钮和一个用了显示信息的TextView,点击“登录”按钮后打开一个新的Activity,新Activity上面有输入用户名的控件,在用户关闭这个Activity后,将用户输入的用户名到主Activity,并显示在主Activity的TextView中。
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.net.Uri;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private static final int SUBACTIVITY1 = 1;
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.show);
final Button btn1 = (Button)findViewById(R.id.bt1);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent(MainActivity.this, SubActivity.class);
startActivityForResult(intent, SUBACTIVITY1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case SUBACTIVITY1:
if (resultCode == RESULT_OK)
{
Uri uriData = data.getData();
textView.setText(uriData.toString());
}
break;
}
}
}
SubActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SubActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subactivity);
final EditText name = (EditText)findViewById(R.id.name);
final EditText psd = (EditText)findViewById(R.id.psd);
Button btnOK = (Button)findViewById(R.id.btnOK);
btnOK.setOnClickListener(new OnClickListener(){
public void onClick(View view){
String uriString = "用户名:"+name.getText().toString()+" 密码:"+psd.getText().toString();
Uri data = Uri.parse(uriString);
Intent result = new Intent(null, data);
setResult(RESULT_OK, result);
finish();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.humphreylin.exp4.MainActivity">
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/bt1"
android:text="登陆"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.242" />
<TextView
android:layout_width="268dp"
android:layout_height="142dp"
android:id="@+id/show"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteY="141dp"
tools:layout_editor_absoluteX="37dp"
app:layout_constraintVertical_bias="0.503"
app:layout_constraintHorizontal_bias="0.503" />
</android.support.constraint.ConstraintLayout>
subactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.humphreylin.exp4.SubActivity">
<LinearLayout
android:layout_width="370dp"
android:layout_height="300dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="370dp"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="用户名:"
android:textSize="25sp" />
<EditText
android:id="@+id/name"
android:layout_width="150dp"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:layout_width="370dp"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:text="密码:"
android:textSize="25sp"/>
<EditText
android:id="@+id/psd"
android:layout_width="150dp"
android:layout_height="50dp" />
</LinearLayout>
<Button
android:id="@+id/btnOK"
android:layout_width="150dp"
android:layout_height="75dp"
android:text="登陆"
android:textSize="25sp"/>
</LinearLayout>
</LinearLayout>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.humphreylin.exp4" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/app_name" android:name=".SubActivity"> </activity>
</application>
</manifest>