在visual studio 2017下用xamarin for android 做个简单的注册页面,包含单选控件,多选控件和下拉菜单控件

照着牛腩老师的视频,自己做了个注册页面,实现了简单的单选控件、多选控件、下拉菜单控件的使用,由于日历控件太丑,就不模仿了,下面就是reg.axml的布局效果,后面是代码。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView1">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="horizontal"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout1"
            android:gravity="center">
            <TextView
                android:text="注册界面"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/textView1" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout2">
            <TextView
                android:text="用户名:"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/textView2" />
            <EditText
                android:inputType="date"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/editText1" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout3">
            <TextView
                android:text="密码:"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/textView3" />
            <EditText
                android:inputType="textPassword"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/editText2" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout4">
            <TextView
                android:text="确认密码:"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/textView4" />
            <EditText
                android:inputType="textPassword"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/editText3" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout5">
            <TextView
                android:text="性别:"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/textView5" />
            <RadioGroup
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/radioGroup1"
                android:orientation="horizontal">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="男"
                    android:id="@+id/radioButton1" />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女"
                    android:id="@+id/radioButton2" />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="不男不女"
                    android:id="@+id/radioButton3" />
            </RadioGroup>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout7">
            <TextView
                android:text="爱好:"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/textView6" />
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout8">
                <CheckBox
                    android:text="体育"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/checkBox2" />
                <CheckBox
                    android:text="音乐"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/checkBox4" />
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout9">
                <CheckBox
                    android:text="购物"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/checkBox5" />
                <CheckBox
                    android:text="电影"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/checkBox6" />
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout10">
                <CheckBox
                    android:text="汽车"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/checkBox7" />
                <CheckBox
                    android:text="财经"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/checkBox8" />
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout11"
            android:gravity="left">
            <TextView
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/textView7"
                android:text="最喜欢的编程语言" />
            <Spinner
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/spinner1" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout6"
            android:gravity="center">
            <Button
                android:text="返回"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/button2" />
            <Button
                android:text="注册"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/button3" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>


RegActivity.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 App5
{
    [Activity(Label = "RegActivity")]
    public class RegActivity : Activity
    {
        //老规矩,先定义控件

Button btn_reg;
        Button btn_return;
        EditText txt_username;
        EditText txt_pwd;
        EditText txt_pwd2;
        RadioButton rad_xbm,rad_xbw,rad_xbmw;
        CheckBox chk_sport,chk_movie, chk_shopping, chk_car, chk_finace, chk_music;
        Spinner sp_inst;
        string xb;
        string whe;


        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Create your application here
            SetContentView(Resource.Layout.reg);

//绑定控件
            btn_reg = FindViewById<Button>(Resource.Id.button3);
            btn_return = FindViewById<Button>(Resource.Id.button2);
            txt_username = FindViewById<EditText>(Resource.Id.editText1);
            txt_pwd = FindViewById<EditText>(Resource.Id.editText2);
            txt_pwd2 = FindViewById<EditText>(Resource.Id.editText3);
            rad_xbm = FindViewById<RadioButton>(Resource.Id.radioButton1);
            rad_xbw = FindViewById<RadioButton>(Resource.Id.radioButton2);
            rad_xbmw = FindViewById<RadioButton>(Resource.Id.radioButton3);
            chk_car = FindViewById<CheckBox>(Resource.Id.checkBox7);
            chk_finace= FindViewById<CheckBox>(Resource.Id.checkBox8);
            chk_sport = FindViewById<CheckBox>(Resource.Id.checkBox2);
            chk_shopping= FindViewById<CheckBox>(Resource.Id.checkBox5);
            chk_movie= FindViewById<CheckBox>(Resource.Id.checkBox6);
            chk_music= FindViewById<CheckBox>(Resource.Id.checkBox4);
            sp_inst = FindViewById<Spinner>(Resource.Id.spinner1);
            btn_reg.Click += Btn_reg_Click;
            btn_return.Click += Btn_return_Click;

//这是下拉菜单的写法,有点复杂,不过这已经是网上最简单的写法了
            List<string> list = new List<string>() { "php", "c#", "photoshop" };
            ArrayAdapter ada = new ArrayAdapter(this, Android.Resource.Layout.SimpleDropDownItem1Line);
            ada.AddAll(list);
            sp_inst.Adapter = ada;


        }


     


        private void Btn_return_Click(object sender, EventArgs e)
        {
              this.Finish();
           
        }
   private void Btn_reg_Click(object sender, EventArgs e)
        {

           //这里用到了三元运算符,可以少写不少if+else(敲黑板 划重点)
            xb = rad_xbm.Checked ? "男" : (rad_xbw.Checked?"女":"不男不女");
           

           //获取下拉菜单的值

     whe = sp_inst.SelectedItem.ToString();
            string returnmessage = "";
            returnmessage += txt_username.Text+"\n";
            returnmessage += txt_pwd.Text + "\n";
            returnmessage += txt_pwd2.Text + "\n";
            returnmessage += xb + "\n";
            returnmessage += whe;
            Toast.MakeText(this, "这是一个弹框!  \n密码 \n" + returnmessage, ToastLength.Long).Show();

        }
    }
}

日行一善~~

猜你喜欢

转载自blog.csdn.net/u014194297/article/details/79097553