Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,025

HOME > Mobile > Android Tutorials - สอนเขียน Android App ฟรี เขียนโปรแกรมแอนดรอยด์บน SmartPhone / Tablets > ตอนที่ 9 : Xamarin กับ Android การสร้าง Dialog Popup และ Alert Dialog / Toast Make Text (C#)



Clound SSD Virtual Server

ตอนที่ 9 : Xamarin กับ Android การสร้าง Dialog Popup และ Alert Dialog / Toast Make Text (C#)

ตอนที่ 9 : Xamarin กับ Android การสร้าง Dialog Popup และ Alert Dialog / Toast Make Text (C#) ในหัวข้อที่ผ่านมาเราได้เรียนรู้การติดตั้ง Xamarin และพื้นฐานไปบ้างพอสมควร เช่นพื้นฐานและเบื้องต้นการสร้าง Create New Project และการปรับแต่งค่าพื้นฐานที่จำเป็นต่อการเขียนโปรแกรมบน Android ด้วย C# บน Framework ของ Xamarin หัวข้อนี้เราจะได้เรียนรู้การเขียนโปรแกรมเกี่ยวกับการโต้ตอบระหว่าง User กับ Android แบบง่าย ๆ ผ่าน AlertDialog และ Make Text แบบ Toast Dialog ซึ่งเป็น Feature ที่สามารถใช้งานได้ง่าย ๆ แต่มีความจำเป็น และสามารถประยุกต์การใช้งานได้มากมาย

AlertDialog Syntax รูปแบบการใช้งานด้วยภาษา C#
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            AlertDialog alert = builder.Create();
            alert.SetTitle("Title");
            alert.SetMessage("Message");
            alert.Show();

Toast MakeText ด้วยภาษา C#
            Toast.MakeText(this, "Message", ToastLength.Short).Show();


ลองมาดูตัวอย่างเพื่อความเข้าใจมากขึ้น

Example 1 การสร้าง AlertDialog และ Popup แบบง่าย ๆ

Xamarin Android C# Dialog Popup Alert Dialog

ก่อนอื่นออกแบบ Layout ง่าย ๆ ดังนี้

Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Simple Dialog" />
    <Button
        android:text="Complext Dialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2" />
</LinearLayout>

MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace myFirstApps
{
    [Activity(Label = "myFirstApps", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

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

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

            // Button Open
            Button btnOpen = FindViewById<Button>(Resource.Id.btnOpen);
            btnOpen.Click += (object sender, EventArgs e) =>
            {
                btnOpenClick(sender, e);
            };

        }


        private void btnOpenClick(object sender, EventArgs e)
        {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            AlertDialog alert = builder.Create();
            alert.SetTitle("Your Title");
            alert.SetMessage("Test message sample.");
            alert.Show();
        }


    }
}

ทดสอบการทำงาน

Xamarin Android C# Dialog Popup Alert Dialog

คลิกที่ Button Open เพื่อเปิด AlertDialog Popup

Xamarin Android C# Dialog Popup Alert Dialog

แสดง AlertDialog แบบง่าย ๆ








Example 2 การสร้าง AlertDialog และการปุ่ม Confirm / Cancel แบบง่าย ๆ และการใช้ Toast MakeText

Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Simple Dialog" />
    <Button
        android:text="Complext Dialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2" />
</LinearLayout>

MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace myFirstApps
{
    [Activity(Label = "myFirstApps", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

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

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

            // Button Open
            Button btnOpen = FindViewById<Button>(Resource.Id.btnOpen);
            btnOpen.Click += (object sender, EventArgs e) =>
            {
                btnOpenClick(sender, e);
            };

        }


        private void btnOpenClick(object sender, EventArgs e)
        {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            AlertDialog alert = builder.Create();
            alert.SetTitle("Your Title");
            alert.SetMessage("Test message sample.");
            alert.SetButton("OK", (s, ev) =>
            {
                Toast.MakeText(this, "You click : OK", ToastLength.Short).Show();
            });
            alert.SetButton2("Cancel", (s, ev) =>
            {
                Toast.MakeText(this, "You click : Cancel", ToastLength.Short).Show();
            });
            alert.Show();
        }


    }
}

ทดสอบการทำงาน

Xamarin Android C# Dialog Popup Alert Dialog

แสดง AlertDialog จะมีปุ่ม OK และ Cancel

Xamarin Android C# Dialog Popup Alert Dialog

เมื่อคลิกที่ OK จะแสดงข้อความ "You click : OK" บน Toast MakeText

Xamarin Android C# Dialog Popup Alert Dialog

เมื่อคลิกที่ Cancel จะแสดงข้อความ "You click : Cancel" บน Toast MakeText



Example 3 การสร้าง AlertDialog และ Input บน Dialog และโต้ตอบผ่าน Toast MakeText

Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Simple Dialog" />
    <Button
        android:text="Complext Dialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2" />
</LinearLayout>









MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace myFirstApps
{
    [Activity(Label = "myFirstApps", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

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

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

            // Button Open
            Button btnOpen = FindViewById<Button>(Resource.Id.btnOpen);
            btnOpen.Click += (object sender, EventArgs e) =>
            {
                btnOpenClick(sender, e);
            };

        }


        private void btnOpenClick(object sender, EventArgs e)
        {
            EditText input = new EditText(this);

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            AlertDialog alert = builder.Create();
            alert.SetTitle("Your Title");
            alert.SetMessage("Input your name : ");
            alert.SetView(input);
            alert.SetButton("OK", (s, ev) =>
            {
                Toast.MakeText(this, "Hi, " + input.Text, ToastLength.Short).Show();
            });
            alert.Show();
        }


    }
}

ทดสอบการทำงาน

Xamarin Android C# Dialog Popup Alert Dialog

AlertDialog และการรับค่า Input

Xamarin Android C# Dialog Popup Alert Dialog

โต้ตอบผ่าน Toast MakeText



Example 4 การสร้าง AlertDialog และการใช้ ListView บน Dialog

Xamarin Android C# Dialog Popup Alert Dialog

Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Simple Dialog" />
    <Button
        android:text="Complext Dialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2" />
</LinearLayout>

MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace myFirstApps
{
    [Activity(Label = "myFirstApps", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

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

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

            // Button Open
            Button btnOpen = FindViewById<Button>(Resource.Id.btnOpen);
            btnOpen.Click += (object sender, EventArgs e) =>
            {
                btnOpenClick(sender, e);
            };

        }

        string[] items;

        private void btnOpenClick(object sender, EventArgs e)
        {
            items = new string[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", };
            ArrayAdapter ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, items);
            var listView = new ListView(this);
            listView.Adapter = ListAdapter;
            listView.ItemClick += listViewItemClick;

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            AlertDialog alert = builder.Create();
            alert.SetTitle("Your Title");
            alert.SetMessage("Select your item.");
            alert.SetView(listView);

            alert.Show();
        }

        void listViewItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            Toast.MakeText(this, "You clicked on : " + items[e.Position], ToastLength.Short).Show();
        }


    }
}




ทดสอบการทำงาน

Xamarin Android C# Dialog Popup Alert Dialog

แสดงรายการ ListView บน Alert Dialog

Xamarin Android C# Dialog Popup Alert Dialog

เมื่อเลือกรายการ ListView บน Alert Dialog

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-09-15 12:38:17 / 2017-03-26 20:56:54
  Download : No files
 Sponsored Links / Related

 
ตอนที่ 1 : รู้จัก Xamarin ติดตั้ง Xamarin เขียน Android Apps ด้วย C# (.Net Framework)
Rating :

 
ตอนที่ 2 : รู้จัก Xamarin Studio IDE การเขียน Android บน Xamarin ด้วยภาษา C#
Rating :

 
ตอนที่ 3 : พื้นฐาน Xamarin บน Visual Studio และการสร้าง Project ของ Android ด้วย C#
Rating :

 
ตอนที่ 4 : ติดตั้ง Xamarin บน Mac (OS X) และการเขียน Android บนเครื่อง Mac ด้วย C#
Rating :

 
ตอนที่ 5 : Xamarin กับ Android โครงสร้างไฟล์ Project และ Controls / Widgets (C#)
Rating :

 
ตอนที่ 6 : Xamarin กับ Android สร้าง Event Handler โต้ตอบแบบง่าย ๆ (C#)
Rating :

 
ตอนที่ 7 : Xamarin กับ Android สร้าง Activity เชื่อมโยง Intent ส่งค่า Pass ตัวแปรระหว่าง Activity(C#)
Rating :

 
ตอนที่ 8 : Xamarin กับ Android การใช้ SetContentView() ควบคุมการแสดง XML Layout (C#)
Rating :

 
ตอนที่ 10 : Xamarin กับ Android การ Generate/Deploy เป็น APK Package นำไปใช้งานจริง (C#)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่