3.11 MonoForAndroid用户人机界面--返回数据到前一个Activity

如果要在次页面上加上一个"回到上一页"的按钮,而非通过模拟器的回复键,且回上一页后又能保留之前输入的相关信息.

那么就必须使用StartActivityForResult()来唤起一个Activity.

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/title"
        android:layout_width="243px"
        android:layout_height="29px"
        android:text="@string/title"
        android:textSize="24sp"
        android:layout_x="36px"
        android:layout_y="32px" />
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="37px"
        android:text="@string/text1"
        android:textSize="18sp"
        android:layout_x="40px"
        android:layout_y="156px" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="29px"
        android:text="@string/text2"
        android:textSize="18sp"
        android:layout_x="40px"
        android:layout_y="102px" />
    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cm"
        android:textSize="18sp"
        android:layout_x="231px"
        android:layout_y="157px" />
    <Button
        android:id="@+id/button1"
        android:layout_width="70px"
        android:layout_height="48px"
        android:text="计算"
        android:layout_x="130px"
        android:layout_y="232px" />
    <RadioGroup
        android:id="@+id/sex"
        android:layout_width="300px"
        android:layout_height="100px"
        android:layout_x="97px"
        android:layout_y="98px"
        android:orientation="horizontal"
        android:checkedButton="@+id/sex1">
        <RadioButton
            android:id="@+id/sex1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男的" />
        <RadioButton
            android:id="@+id/sex2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女的" />
    </RadioGroup>
    <EditText
        android:id="@+id/height"
        android:layout_width="130px"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_x="96px"
        android:layout_y="142px"
        android:numeric="decimal" />
</AbsoluteLayout>

myalyout.axml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_x="50px"
        android:layout_y="72px" />
    <Button
        android:id="@+id/button1"
        android:layout_width="100px"
        android:layout_height="48px"
        android:text="回上一頁"
        android:layout_x="110px"
        android:layout_y="180px" />
</AbsoluteLayout>


MainActivity.cs

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Ex03_11
{
    [Activity(Label = "Ex03_11", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private EditText et; 
        private RadioButton rb1; 
        private RadioButton rb2;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
           
            Button b1 = (Button)FindViewById(Resource.Id.button1);
            b1.Click += delegate { 

                                    /*取得输入的身高*/ 
                                    et = (EditText) FindViewById(Resource.Id.height); 
                                    double height=Double.Parse(et.Text.ToString()); 
                                     /*取得选择的性别*/ 
                                    String sex=""; 
                                    rb1 = (RadioButton) FindViewById(Resource.Id.sex1); 
                                    rb2 = (RadioButton) FindViewById(Resource.Id.sex2); 
                                    if(rb1.Checked) 
                                    { 
                                        sex="M"; 
                                    }
                                    else
                                    { 
                                        sex="F"; 
                                    } 
                                    /*new一个Intent对象,并指定class*/ 
                                    Intent intent = new Intent(); 
                                    //intent.setClass(EX03_11.this,EX03_11_1.class); 
                                    intent.SetClass(this, typeof(Activity1));
                                    /*new一个Bundle对象,并将要传递的数据传入*/ 
                                    Bundle bundle1 = new Bundle(); 
                                    bundle1.PutDouble("height",height); 
                                    bundle1.PutString("sex",sex); 

                                    /*将Bundle对象assign给Intent*/ 
                                    intent.PutExtras(bundle1); 
                                    /*调用Activity EX03_11_1*/ 
                                    //startActivityForResult(intent,0); } }); 
                                    StartActivityForResult(intent,0);
                                };

            }

        /* 重写 onActivityResult()*/ 
        override protected void OnActivityResult(int requestCode, Result  resultCode, Intent data) 
        { 
            switch (resultCode) 
            { 
                    
                case Result.Ok: 
                /* 取得数据,并显示于画面上 */ 
                Bundle bunde = data.Extras; 
                   
                String sex = bunde.GetString("sex"); 
                double height = bunde.GetDouble("height"); 
                et.SetText(""+height,TextView.BufferType.Normal); 
                if(sex.Equals("M")) 
                { 
                    rb1.Checked =true ;
                }
                else 
                { 
                    rb2.Checked=true ;
                } 
                break; 
                default: 
                    break; 
            }
        }
 
    }
}


Activity1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Ex03_11
{
    [Activity(Label = "Activity1")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            /* 加载main.xml Layout */
            SetContentView(Resource.Layout.myalyout);

            /* 取得Intent中的Bundle对象 */
            Bundle bunde = this.Intent.Extras;
            /* 取得Bundle对象中的数据 */
            string sex = bunde.GetString("sex");
            double height = bunde.GetDouble("height");
            /*判断性别 */
            String sexText = "";
            if (sex.Equals("M"))
            {
                sexText = "男性";
            }
            else
            {
                sexText = "女性";
            }

            /* 取得标准体重 */
            string weight = this.getWeight(sex, height);
            /* 设定输出文字 */
            TextView tv1 = (TextView)FindViewById(Resource.Id.text1);
            tv1.SetText("你是一位" + sexText + "\n你的身高是" + height +
                        "公分\n你的标准体重是" + weight + "公斤", TextView.BufferType.Normal);

            Button button = FindViewById<Button>(Resource.Id.button1);

            button.Click += delegate { 
                /* 回传result回上一个activity */ 
                this.SetResult(Result.Ok,this.Intent); 
                /* 关闭activity */ 
                this.Finish();
            };


        }
        private String getWeight(String sex, double height)
        {
            String weight = "";
            if (sex.Equals("M"))
            {
                weight = ((height - 80) * 0.7).ToString();
            }
            else
            {
                weight = ((height - 70) * 0.6).ToString();
            }
            return weight;
        }
    }
}



在这个问题中,其中一个软件包需要使用Python 3.11的ABI(应用二进制接口),而具体的软件包是python3.11-psycopg2-2.9.3-1.el8.aarch64。 首先,Python ABI是指Python解释器与其他模块之间的接口协议,用于确保模块可以与特定版本的Python解释器兼容。在这种情况下,软件包python3.11-psycopg2-2.9.3-1.el8.aarch64需要与Python 3.11的ABI兼容。 这意味着,如果你想安装软件包python3.11-psycopg2-2.9.3-1.el8.aarch64,你需要使用Python 3.11版本或更高的版本。原因是软件包在编译时使用了Python 3.11的ABI,如果你使用较低版本的Python,它将无法正常工作。 要解决这个问题,你可以执行以下操作之一: 1. 升级Python版本:更新到Python 3.11或更新的版本。这样做可能需要你手动安装适用于你的操作系统的Python 3.11版本。 2. 寻找兼容的软件包版本:查找软件包的其他可用版本,其中包括与你当Python版本兼容的版本。可以尝试在软件包的官方网站或软件包管理系统上查找。 3. 尝试其他解决方案:如果你无法升级Python版本或找到兼容的软件包版本,可以尝试其他解决方案。例如,可以考虑使用适用于你当Python版本的另一个相似的软件包。 总之,要解决这个问题,你需要确保软件包python3.11-psycopg2-2.9.3-1.el8.aarch64与Python 3.11的ABI兼容,这可能需要你升级Python版本或寻找兼容的软件包版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值