您的位置:首页 > 移动开发 > Android开发

Android使用两个Activity页面切换… 分类: Android开发 2014-05-30 10:55 70人阅读 评论(0) 收藏

2014-05-30 10:55 666 查看
完成大写转换并返回结果
两个页面XML文件如下:
1.activity_main.xml:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/editText1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10" >

android:id="@+id/button1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="转换" />

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView" />

2.slave.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/button1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="转为大写" />

两个代码如下

1.MainActivity.java

package com.leansmall.mydeom;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

private EditText eText;
private Button
button;
private TextView
tText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eText=(EditText)this.findViewById(R.id.editText1);
tText=(TextView)this.findViewById(R.id.textView1);
button=(Button)this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Bundle bundle=new Bundle();
bundle.putString("value",eText.getText().toString());
Intent intent=new Intent(MainActivity.this,slave.class);
intent.putExtras(bundle);
startActivityForResult(intent, 10);
}
});

}
protected void onActivityResult(int requestCode,int
resultCode,Intent Data) {
switch (requestCode)
{
case 10:
Bundle bundle=Data.getExtras();

tText.setText(bundle.getString("result"));

}

}

}

2.slave.java

package com.leansmall.mydeom;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class slave extends Activity {

private Button
button;
String value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slave);

button=(Button)this.findViewById(R.id.button1);
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
value=bundle.getString("value");
value=value.toUpperCase();
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent intent=new
Intent();

intent.putExtra("result",
value);

slave.this.setResult(RESULT_OK,intent);

slave.this.finish();
}
});

}

}

注意:两个acitvity都需要在AndroidManifest.xml的application中注册才可以使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐