[XAMARIN] Entry, Editor, SearchBar DatePicker, TimePicker 사용 예제

2019. 6. 8. 22:21IT/C#

Entry

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ch03" x:Class="ch03.MainPage"
             Padding="20"
             Appearing="Handle_Appearing"
             >
    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="아이디 : " />
        <Entry x:Name="txtID" Margin="0,-10,0,0" 
               Placeholder="아이디 혹은 이메일 입력"
               Keyboard="Email"
               Completed="Handle_Completed"
               TextChanged="Handle_TextChanged"
               />
        
        <Label Text="암호 : " Margin="0,20,0,0" />
        <Entry x:Name="txtPWD" Margin="0,-10,0,0" 
               IsPassword="true"
               Placeholder="비밀번호 입력"
               Keyboard="Default"
               Completed="Handle_Completed_1"
               />
        
        <Label Text="OTP 암호 : " Margin="0,40,0,0" />
        <Entry x:Name="txtOTP" Margin="0,-10,0,0" 
               IsPassword="true"
               Placeholder="OTP 번호 입력"
               Keyboard="Numeric"
               />
        
        <Editor x:Name="ediText" Placeholder="간단한 자기소개를 적어주세요"
                Margin="0,-10,0,0"
                
                />
        
        <Button x:Name="btnLogin" Text="로그인" Margin="0,20,0,0" Clicked="Handle_Clicked" />
        <Button Text="회원가입" />
    </StackLayout>
</ContentPage>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace ch03
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(true)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        void Handle_Appearing(object sender, System.EventArgs e)
        {
            this.txtID.Focus();
        }

        void Handle_Completed(object sender, System.EventArgs e)
        {
            txtPWD.Focus();
        }

        void Handle_Completed_1(object sender, System.EventArgs e)
        {
            btnLogin.Focus();
        }

        void Handle_Clicked(object sender, System.EventArgs e)
        {
            this.DisplayAlert("로그인", String.Format("아이디{0}, 패스워드{1}", txtID.Text, txtPWD.Text), "확인");

        }

        void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
        {
            Entry txt = (Entry)sender;
            Debug.WriteLine("=====> 24 : ", txt.Text);
        }
    }
}

 

Editor

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:SimNotepad" x:Class="SimNotepad.MainPage"
             Title="Simple Note Ver 1.0.1"
             >
    <StackLayout>
        <!-- Place new controls here -->
        <AbsoluteLayout VerticalOptions="FillAndExpand">
            <Editor x:Name="txtNote"
                    Keyboard="Text"
                    AbsoluteLayout.LayoutBounds="0,0,1,1"
                    AbsoluteLayout.LayoutFlags="All"
                    />
        </AbsoluteLayout>
    </StackLayout>
</ContentPage>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace SimNotepad
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(true)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            if(Application.Current.Properties.ContainsKey("note_data") == true)
            {
                txtNote.Text = (string)Application.Current.Properties["note_data"];
            }

        }

        public void SaveData()
        {
            Application.Current.Properties["note_data"] = txtNote.Text;
        }
    }
}
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace SimNotepad
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new MainPage());
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
            ((MainPage)((NavigationPage)Application.Current.MainPage).CurrentPage).SaveData();
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

 

SearchBar, DatePicker, TimePicker

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ch5_03" x:Class="ch5_03.MainPage">
    <StackLayout>
        <!-- Place new controls here -->
        <SearchBar x:Name="sb"
                   Placeholder="검색 문자열 입력 "
                   CancelButtonColor="Red"
                   SearchButtonPressed="Handle_SearchButtonPressed"
                   TextChanged="Handle_TextChanged"
                   />
        
        <DatePicker MinimumDate="2019-01-01"
                    MaximumDate="2019-12-31"
                    DateSelected="Handle_DateSelected"
                    Format="yyyy-MM-dd"
                    />
        
        <StackLayout Orientation="Horizontal" VerticalOptions="Start">
            <Label Text="계약일" VerticalOptions="Center"/>
            <DatePicker x:Name="dtStart"
                        Format="yyyy-MM-dd"
                        DateSelected="dtStart_DateSelected"
                        />
        
            <Label Text="완료일" VerticalOptions="Center"/>
            <DatePicker x:Name="dtEnd"
                        Format="yyyy-MM-dd"
                        DateSelected="dtEnd_DateSelected"
                        />
            <Label x:Name="lbl01" Text="" VerticalOptions="Center" />
        </StackLayout>
        
        <StackLayout>
            <TimePicker x:Name="tmPicker"
                        PropertyChanged="tmPicker_PropertyChanged"
                        />
            <Switch x:Name="swTime"
                    Toggled="swTime_Toggled"
                    />
        </StackLayout>
    </StackLayout>
</ContentPage>

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace ch5_03
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(true)]
    public partial class MainPage : ContentPage
    {
        DateTime desTime;

        public MainPage()
        {
            InitializeComponent();

            Device.StartTimer(TimeSpan.FromSeconds(1), tmr_Timer);

        }

        private bool tmr_Timer()
        {
            if(swTime.IsToggled == true && DateTime.Now >= desTime)
            {
                swTime.IsToggled = false;
                this.DisplayAlert("알림", "약속 시간 입니다.", "확인");
            }
            Debug.WriteLine("34 - curt time" + DateTime.Now.ToLongTimeString());
            Debug.WriteLine("35 - dest time" + desTime.ToLongTimeString());
            return true;
        }

        void Handle_SearchButtonPressed(object sender, System.EventArgs e)
        {
            Debug.WriteLine("=======> 23 : button pressed ", sb.Text);
        }

        void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
        {
            Debug.WriteLine("=======> 28 : ", sb.Text);
        }

        void Handle_DateSelected(object sender, Xamarin.Forms.DateChangedEventArgs e)
        {
            Debug.WriteLine(((DatePicker)sender).Date.ToShortDateString());
        }

        void dtEnd_DateSelected(object sender, Xamarin.Forms.DateChangedEventArgs e)
        {
            calcDate();
        }

        void dtStart_DateSelected(object sender, Xamarin.Forms.DateChangedEventArgs e)
        {
            calcDate();
        }

        private void calcDate()
        {
            DateTime start = dtStart.Date;
            DateTime end = dtEnd.Date;

            lbl01.Text = String.Format("개발기간:{0}일 ", (end - start).Days);
        }

        void tmPicker_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SetDesTime();
        }

        void swTime_Toggled(object sender, Xamarin.Forms.ToggledEventArgs e)
        {
            SetDesTime();
        }

        private void SetDesTime()
        {
            // 알람이 울려야 되는 시간
            desTime = DateTime.Today + tmPicker.Time;

            if(swTime.IsToggled == true)
            {
                // 현재 2018/5/2  02:00
                desTime = DateTime.Today + tmPicker.Time;

                // 2018/05/2 23:00

                if(desTime <= DateTime.Now)
                {
                    desTime += TimeSpan.FromDays(1);
                }


            }

        }
    }
}