MainActivity文件代码:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private final static int SUBACTIVITY=1;
private final static int SUBACTIVITY1=2;
EditText editText01;
EditText editText02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView01=(TextView)findViewById(R.id.textView01);
final TextView textView02=(TextView)findViewById(R.id.textView02);
editText01=(EditText)findViewById(R.id.edit01);
editText02=(EditText)findViewById(R.id.edit02);
final Button button1=(Button)findViewById(R.id.btn1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(editText02.getText().toString().equals("123456")){
Intent intent=new Intent(MainActivity.this,SubActivity.class);
startActivityForResult(intent,SUBACTIVITY);
}
else{
Intent intent=new Intent(MainActivity.this,SubActivity1.class);
startActivityForResult(intent,SUBACTIVITY1);
}
}
});
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
switch (requestCode){
case SUBACTIVITY:
editText01.setText("");
editText02.setText("");
break;
case SUBACTIVITY1:
break;
}
}
}
子Activity SubActivity文件代码:
package com.example.daisy.test4;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SubActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subactivity);
final TextView textView01=(TextView)findViewById(R.id.textShow01);
final Button btn_change=(Button)findViewById(R.id.btn_change);
btn_change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED,null);
finish();
}
});
}
}
子Activity SubActivity1文件:
package com.example.daisy.test4;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SubActivity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subactivity1);
Button btn_again=(Button)findViewById(R.id.btn_again);
btn_again.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED,null);
finish();
}
});
}
}
MainActivity的布局文件activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView android:id="@+id/textView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textAlignment="center"
android:textSize="20dp" />
<EditText android:id="@+id/edit01"
android:layout_width="150dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView android:id="@+id/textView02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textAlignment="center"
android:textSize="20dp" />
<EditText android:id="@+id/edit02"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</TableRow>
</TableLayout>
<Button android:id="@+id/btn1"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="登录"
android:textSize="20dp"/>
</LinearLayout>
SubActivity的布局文件subactivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textShow01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录成功"
android:textSize="20dp" />
<Button
android:id="@+id/btn_change"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="切换账户"
android:textSize="20dp"/>
</LinearLayout>
SubActivity1的布局文件subactivity1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textShow01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码错误"
android:textSize="20dp" />
<Button
android:id="@+id/btn_again"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="重新登录"
android:textSize="20dp"/>
</LinearLayout>