Android使用两个Activity页面切换… 分类: Androi...

本文详细介绍了在Android应用开发中如何通过两个XML文件和对应的Java代码实现大写转换的功能,包括布局设计、事件监听以及数据传递。
完成大写转换并返回结果
两个页面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中注册才可以使用

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

转载于:https://www.cnblogs.com/leansmall/p/4715944.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值