要判斷EditText有無輸入值,可用以下方式判斷
if("".equals(editText.getText().toString().trim()))
{
}
或可用
editText.getText().length()== 0
editText.getText().toString() == null
editText.getText().toString().equals("")
2015年12月3日 星期四
2015年10月19日 星期一
NTP SERVER LIST
要使用網路對時,可使用下列伺服器
tick.stdtime.gov.tw
tock.stdtime.gov.tw
time.stdtime.gov.tw
clock.stdtime.gov.tw
watch.stdtime.gov.tw
2015年10月13日 星期二
使用ajax非同步更新網頁資料
至https://jquery.com/
下載jquery,複製到網站伺服器上
在<head>中引用jquery
<script type="text/javascript" src="jquery-2.1.4.js"></script>
要非同步更新的物件
<span id="LiveUsers" style="color:#99FF33;">目前人數0,總人數0</span>
計算人數的程式寫在ashx上,回傳字串為"1,1"使用java script,至每隔一秒更新資料
<script type="text/javascript">
//至ashx取出回應的資料,
function UserCountValue() {
$.ajax({
url: "VideoUserCount.ashx", //程式路徑
contentType: "text/plain", //回傳資型態
success: function (rvalue) {
var a = rvalue.split(','); //拆解字串取出要顯示的值
$("#LiveUsers").text("目前人數 " + a[0] + ", 總人數 " + a[1]);
},
})
}
//第一次啟動網頁時先執行一次
UserCountValue();
//每隔一秒更新一次
setInterval("UserCountValue()", 1000);
</script>
下載jquery,複製到網站伺服器上
在<head>中引用jquery
<script type="text/javascript" src="jquery-2.1.4.js"></script>
要非同步更新的物件
<span id="LiveUsers" style="color:#99FF33;">目前人數0,總人數0</span>
計算人數的程式寫在ashx上,回傳字串為"1,1"使用java script,至每隔一秒更新資料
<script type="text/javascript">
//至ashx取出回應的資料,
function UserCountValue() {
$.ajax({
url: "VideoUserCount.ashx", //程式路徑
contentType: "text/plain", //回傳資型態
success: function (rvalue) {
var a = rvalue.split(','); //拆解字串取出要顯示的值
$("#LiveUsers").text("目前人數 " + a[0] + ", 總人數 " + a[1]);
},
})
}
//第一次啟動網頁時先執行一次
UserCountValue();
//每隔一秒更新一次
setInterval("UserCountValue()", 1000);
</script>
如需動態產生java script,可使用Page.ClientScript
在後置程式碼中,要動態產生java script可使用Page.ClientScript
共有三種模式
Page.ClientScript.RegisterClientScriptBlock
Page.ClientScript.RegisterStartupScript
Page.ClientScript.RegisterClientScriptInclude
Page.ClientScript.RegisterClientScriptBlock會產生在<form>標籤的下面
Page.ClientScript.RegisterClientScriptInclude會產生在<form>與</form>之間
Page.ClientScript.RegisterStartupScript會產生在</form>標籤的上面
範例
string ScriptText = @"alert('ok');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "自訂名稱",ScriptText, true);
共有三種模式
Page.ClientScript.RegisterClientScriptBlock
Page.ClientScript.RegisterStartupScript
Page.ClientScript.RegisterClientScriptInclude
Page.ClientScript.RegisterClientScriptBlock會產生在<form>標籤的下面
Page.ClientScript.RegisterClientScriptInclude會產生在<form>與</form>之間
Page.ClientScript.RegisterStartupScript會產生在</form>標籤的上面
範例
string ScriptText = @"alert('ok');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "自訂名稱",ScriptText, true);
要取得其他網頁資料,可用 HttpWebRequest
在網頁中如有部份資訊來自其他網頁,可用HttpWebRequest來取得其他網頁資料,
以下範例使用ashx抓取某個網頁的xml資料,再取出想要的部份資料
// 建立HttpWebRequest物件
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://...");
//有需要登入的網頁,需增加登入帳號
myHttpWebRequest.Credentials = new NetworkCredential("帳號", "密碼");
//取得HttpWebResponse物件
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//取得關於HttpWebResponse資料流
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// 建立資料流讀取物件
StreamReader readStream = new StreamReader(receiveStream, encode);
string ReadString = readStream.ReadToEnd();
//將資料流轉為xml文件
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(ReadString);
//找出預讀取的元素
XmlNodeList NodeList = xmlDoc.SelectNodes("WowzaStreamingEngine/VHost/Application");
//使用迴圈取出資料
foreach (XmlNode xn in NodeList)
{
if (xn["Name"].InnerText == "live")
{
context.Response.Write(xn["ConnectionsCurrent"].InnerText+",");
context.Response.Write(xn["ConnectionsTotal"].InnerText);
}
}
//關閉 myHttpWebResponse及 readStream
myHttpWebResponse.Close();
readStream.Close();
以下範例使用ashx抓取某個網頁的xml資料,再取出想要的部份資料
// 建立HttpWebRequest物件
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://...");
//有需要登入的網頁,需增加登入帳號
myHttpWebRequest.Credentials = new NetworkCredential("帳號", "密碼");
//取得HttpWebResponse物件
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//取得關於HttpWebResponse資料流
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// 建立資料流讀取物件
StreamReader readStream = new StreamReader(receiveStream, encode);
string ReadString = readStream.ReadToEnd();
//將資料流轉為xml文件
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(ReadString);
//找出預讀取的元素
XmlNodeList NodeList = xmlDoc.SelectNodes("WowzaStreamingEngine/VHost/Application");
//使用迴圈取出資料
foreach (XmlNode xn in NodeList)
{
if (xn["Name"].InnerText == "live")
{
context.Response.Write(xn["ConnectionsCurrent"].InnerText+",");
context.Response.Write(xn["ConnectionsTotal"].InnerText);
}
}
//關閉 myHttpWebResponse及 readStream
myHttpWebResponse.Close();
readStream.Close();
2015年7月21日 星期二
C#民國年西元年轉換
西元年轉民國年時,如果使用addyear(-1911)的方式,會有閏年的問題,例如2012/2/29使用addyear(-1911)會變成101/2/28,使用字串拆解的方式又過於冗長,
NET Framework有提供方便的轉換方式,來解決這問題
西元年轉民國年
System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar();
DateTime dt = DateTime.Now;
Response.Write(dt.ToString(tc));
西元年日期驗證
DateTime dt = DateTime.Now;
DateTime.TryParse("2015/7/21", out dt) ;
民國年轉西元年
System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar(); Response.Write(DateTime.Parse("104/7/21",tc).Date.ToString("d"));
民國年日期驗證
System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar();
DateTime result;
try
{
result = DateTime.ParseExact("104/7/21", "d", tc);
Response.Write("Ok");
}
catch (FormatException)
{
Response.Write("Wrong");
}
西元年轉民國年
System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar();
DateTime dt = DateTime.Now;
Response.Write(dt.ToString(tc));
西元年日期驗證
DateTime dt = DateTime.Now;
DateTime.TryParse("2015/7/21", out dt) ;
民國年轉西元年
System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar(); Response.Write(DateTime.Parse("104/7/21",tc).Date.ToString("d"));
民國年日期驗證
System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanCalendar();
DateTime result;
try
{
result = DateTime.ParseExact("104/7/21", "d", tc);
Response.Write("Ok");
}
catch (FormatException)
{
Response.Write("Wrong");
}
訂閱:
文章 (Atom)