3/24/2559

การเชื่อมต่อหน้าใน ionic framework

Ionic framework นับว่าเป็น frame work สำหรับพัฒนา mobile application ที่มีประสิทธิภาพมาก เหมาะสำหรับการพัฒนา mobile application ด้วย html และ javascript
ปัญหาสำหรับนักพัฒนาที่เพิ่งใช้ ionic frawork คือการ link หน้าต่างๆใน application ในที่นี้คือตัวอย่างการ link หน้า app สำหรับ ionic framework

1. Download Ionic Framework  จาก http://projectsoft.biz/ionic.zip และแตกโฟลเดอร์ ชื่อ ionic ไว้ใน folder เดียวกับ index.html (ไฟล์เริ่มต้น)

2. สร้างไฟล์ index ดังนี้
<!DOCTYPE html>
<html ng-app="ionicApp">

  <head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">

<!--  เรียกใช่ ionic framework -->
   <link rel="stylesheet" type="text/css" href="ionic/css/ionic.min.css">
   <script type="application/javascript" src="ionic/js/ionic.bundle.js"></script>
 <!--  เรียกใช่ ionic framework -->

  </head>

  <body  ng-controller="main_control" >
   
    <!--- สร้างวิว สำหรับ แสดงเนื้อหา app  -->
    <ion-nav-view></ion-nav-view>
 
<!-- เรียกไฟล์หน้า app แต่ละหน้า -->
    <script src="page1.html" type="text/ng-template"></script>
    <script src="page2.html" type="text/ng-template"></script>

<!---เรียกไฟล์หน้า app--->
    <script src="index.js"></script>
    <script src="page1.js"></script>
    <script src="page2.js"></script>

             
  </body>

</html>

2. สร้างไฟล์ index.js สำหรับควบคุมหน้า index.html

var app=angular.module('ionicApp', ['ionic']);
app.config(function($stateProvider,$urlRouterProvider) {
 
   //กำหนด state ให้แต่ละ page
  //ในที่นี้ page1 สำหรับ ไฟล์ page1.html และ page2  สำหรับไฟล์ page2.html
  $stateProvider
  .state('page1', {
     url: "/",
    templateUrl: 'page1.html'
  })
  .state('page2', {
     url: "/",
    templateUrl: 'page2.html'
  });

 
 
});
app.controller('main_control', function($scope,$state) {

    $state.transitionTo("page1");//กำหนดให้แสดงหน้า page1 เป็นหน้าแรก
   
});


3. สร้างไฟล์ page1.html
<ion-view >
 <div ng-controller="page1_control">
<div class="bar bar-positive"><!-- กำหนด header ให้กับ หน้า app -->
 <h1 class="title">หน้า 1</h1>
</div>
<ion-content class="content has-header " ><!--- แสดงเนื้อหาในหน้า app --->
<div class="padding"  >
<h1>หน้า 1</h1>
<!-- สร้างปุ่มไปที่หน้า 2  -->
<button  ng-click='go_page2()'
class="button icon-right ion-chevron-right button-assertive">หน้า 2</button>

   </div>
</ion-content>
</div>
</ion-view>
<script>
//สร้างส่วน control สำหรับหน้า page1


app.controller('page1_control', function($scope,$state) {

    $scope.go_page2=function()//กำหนดให้เมื่อกด ปุ่มแล้วจะ link ไปหน้า page2
    {
    $state.transitionTo("page2");
    }
 

});

</script>

3. สร้างหน้า page2.html

<ion-view >
 <div ng-controller="page2_control"> 
<div class="bar bar-positive">
 <h1 class="title">หน้า 2</h1><!-- กำหนด header ให้กับ หน้า app  -->
</div>

 
<ion-content class="content has-header " >



<div class="padding"  >
<h1>หน้า 2</h1>
<!-- สร้างปุ่ม link ไปที่หน้า 1  -->
<buttpn  ng-click='go_page1()' class="button icon-left ion-chevron-left button-assertive">หน้า 1</button>
   </div>
       
</ion-content>



</div>

</ion-view>


<script>
//สร้างส่วน control สำหรับหน้า page2
app.controller('page2_control', function($scope,$state) {

    $scope.go_page1=function()
    {
    $state.transitionTo("page1");//กำหนดให้เมื่อกด ปุ่มแล้วจะ link ไปหน้า page1
    }
 

});
</script>

ผลลัพธ์ที่ได้คือ
หน้า 1


หน้า 2

3/18/2559

SharedPreferences ตัวแปรที่ใช้ได้หลาย Activity สำหรับเก็บข้อมูลลงใน Android คล้าย Session ของ PHP


สำหรับ Web Programmer ที่เคยใช้ PHP นั้น ย่อมคุ้นชินกับการใช้ตัวแปร ประเภท Session ที่เก็บค่าตัวแปรแล้วสามารถเข้าถึงตัวแปรใน File อื่นได้ สำหรับการเขียนโปรแกรมบน Android นั้นเราสามารถทำแบบนั้นได้เช่นกันโดยใช้ Class ที่ชื่อว่า SharedPreferences

//สร้าง object SharedPreferences สำหรับเก็บข้อมูล
SharedPreferences sp = getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
//PREF_NAME เป็นชื่อ Shared Preferences ใช้ชื่ออะไรก็ได้
//Context.MODE_PRIVATE คือบอกว่าจะใช้แค่ในแอปพลิเคชันนี้เท่านั้น


String id =sp.getString("id", "0");
//อ่านค่าจาก object preferences โดยให้ค่า 0 (Default) หากยังไม่ได้กำหนด SharedPreferences

SharedPreferences.Editor editor = sp.edit();
editor.putString("id","1");//กำหนดค่าให้กับ SharedPreferences ที่ชื่อ  id  เป็น 1
editor.commit();//บันทึกค่า SharedPreferences

String id =sp.getString("id", "0");//ในกรณีนี้ id จะมีค่าเป็น 1 เพราะได้มีการกำหนดค่าแล้ว

การหาวันที่ถัดไป ด้วย javascript

ในบางโปรเจค ระบบต้องการรู้ว่าวันที่ถัดไป n วันนั้นเป็นวันที่เท่าไหร่ เช่นในระบบเช่า ถ้าเช่าจำนวน n วันระบบต้องแสดงว่าต้องคืนในวันที่เท่าไหร่ เราสามารถใช้ javascritp หาได้ว่าวันถัดไปนั้นที่วันที่เท่าไหร่ดังตัวอน่างต่อไปนี้



var today=new Date();//สร้างตัวแปรวันที่ปัจจุบัน
var tomorrow = new Date();//;สร้างตัวแปรวันพรุ่งนี้

tomorrow.setDate(today.getDate()+1); //กำหนดให้ tomorrow  คือวันถัดไป

document.write(tomorrow.getDate());//พิมพ์วันที่   
document.write((tomorrow.getMonth()+1));
//พิมพ์เดือน  โดย javascript นั้นจะกำหนดให้ เดือนเริ่มต้นที่ 0  จึงต้องบวกด้วย 1
document.write(tomorrow.getFullYear());//พิมพ์ปี