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

完成大写转换并返回结果
两个页面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: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: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中注册才可以使用

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2014-05-30 10:55  leansmall  阅读(102)  评论(0编辑  收藏  举报