2015年12月27日 星期日

[Android] 抓到螢幕的長寬像素

兩個都可以抓的到width 和 Height

使用時機不太一樣


  1.  
  2. WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
  3. Display display = wm.getDefaultDisplay();
  4. Point point= new Point();
  5. display.getSize(point);
  6. int screenHeight =point.y;
  7. int screenWidth =point.x;


我是寫在Fragment 裡面的,要寫在Activity 那getActivity()要拿掉

  1. Display display = getActivity().getWindowManager().getDefaultDisplay();
  2. DisplayMetrics metrics = new DisplayMetrics();
  3. display.getMetrics(metrics);
  4. int screenHeight = metrics.heightPixels;
  5. int screenWidth = metrics.widthPixels;
  6. //這樣也可以
  7. final int h = getResources().getDisplayMetrics().heightPixels;
  8. final int w = getResources().getDisplayMetrics().widthPixels;

2015年12月8日 星期二

[Android] Visibility屬性

Visibility 屬性有三個

  • 可見 Visible
  • 不可見 Invisible
  • 隱藏 Gone
設置方法 :

XML : android:visibility = "visible"
Java : View.setVisibility(VIEW.VISIBLE);


差別:

Invisible : 元素保有空間,只是你看不見

Gone : 元素沒有預留空間,當不存在

2015年10月27日 星期二

[Android]利用LayoutInflater動態添加View

http://blog.csdn.net/guolin_blog/article/details/12921889

[C#] DataTable 實現SQL Distinct


DataTable BoardbyMenu = MyDataTable.DefaultView.ToTable(true, "Menu_ID");


來看一下 ToTable的參數:

ToTable ( bool distinct , params string[] columnNames ) ;

第一個true 過濾重複資料

第二個是欄位名稱,可放多個欄位 寫法:

new string[] { "Menu_ID , name" }




2015年10月15日 星期四

[Jquery]Ajax表單POST 多次

Ajax表單無故POST 兩次 , 弄了好久...

 發現罪魁禍首 是這隻.js jquery.unobtrusive-ajax

因為included 2 次 所以也POST 2 次

參考這篇

2015年10月12日 星期一

[C#][MVC] 資料寫進DB時,檢查是否有Null

create 是要準被塞進DB裡的物件 當create裡面屬性有 null 時 , 就給空值 , 防止DBNull 錯誤發生
  1. IList properties = typeof(ProductSize).GetProperties().ToList();
  2. foreach (var p in properties)
  3. {
  4. if (p.GetValue(create) == null && p.PropertyType == typeof(string))
  5. {
  6. p.SetValue(create, "");
  7. }
  8. }

2015年10月5日 星期一

[C#]MVC 關於刪除資料RemoveRange Remove

  1. var PTryReport = db.ProductTryReport.Where(x => x.ProductSize_ID == Check.ID);
  2. if (PTryReport.Any())
  3. {
  4. //原本這樣寫 會出錯交易失敗錯誤
  5. //因為刪除del就會改變PTryReport的值
  6. foreach (var del in PTryReport)
  7. {
  8. db.ProductTryReport.Remove(del);
  9. db.SaveChanges();
  10. }
  11. }
  12. //理想的寫法 用 RemoveRange 一次刪除全部資料
  13. if (PTryReport.Any())
  14. {
  15. db.ProductTryReport.RemoveRange(PTryReport);
  16. db.SaveChanges();
  17. }

2015年9月17日 星期四

[C#] Split 分割字串

用法一 :最普通的字串分割

split() 括號記得用單引號包起來


  1. string tmp = "AA#BB#CC";
  2. string[] strSplit = tmp.Split('#');
  3. string GetValue = strSplit[0]; // 拿到 AA
  4. GetValue = tmp.Split('#')[1]; //直接拿到字串 BB
  5. //多字元分割
  6. string tmp2 = "AA#|BB$$CC#|";
  7. string[] strSplit2 = tmp2.Split(new string[] { "#|","$$" }, StringSplitOptions.RemoveEmptyEntries);
  8. string GetValue2 = "";
  9. foreach(string s in strSplit2)
  10. {
  11. GetValue2 += s + " "; //拿到 AA BB CC
  12. }
  13.  
  14. //20160311 新增 分割換行的寫法
  15. string[] NewLine = tmp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
  16. //textarea 會把換行符號轉成\n 所以也可以這樣寫
  17. string[] NewLine2 = tmp.products.Replace("\n","|").Split('|');

2015年8月6日 星期四

[Android]初學 使用Intent在Activity傳值(二)

Bundle的傳值,只是單純的A-->B , B-->C 單向傳值 

不像上一篇講到的AB互傳溝通

因此Bundle傳值時,給出的畫面是一個新的、乾淨的初始畫面

範例:


public class MainActivity extends AppCompatActivity {
private Button widget2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
widget2 =(Button)findViewById(R.id.btnNext2);
//接收SecondActivity bundle傳來的參數
Bundle param = getIntent().getExtras();
if(param !=null)
{
setTitle(param.getString("bparam"));
}
//利用bundle傳參數到ScondActivity
widget2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent2 = new Intent();
Bundle bundle= new Bundle();
bundle.putString("sourse","bundle from Main");
intent2.setClass(MainActivity.this, widgetActivity2.class);
intent2.putExtras(bundle);
startActivity(intent2);
}
});
}
}
view raw Main.java hosted with ❤ by GitHub
public class SecondActivity extends AppCompatActivity {
private Button prev;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.widget2);
//接收Main 傳來的參數
Bundle bundle = getIntent().getExtras();
if(bundle !=null)
{
setTitle(bundle.getString("sourse"));
}
prev =(Button)findViewById(R.id.button1);
//Bundle傳送參數到Main
prev.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(widgetActivity2.this, MainActivity.class);
// Bundle 傳送參數
Bundle param = new Bundle();
param.putString("bparam", "from widget2");
intent.putExtras(param);
startActivity(intent);
}
});
view raw Second.java hosted with ❤ by GitHub

[Android]初學 使用Inten在Activity傳值(一)

Main畫面:

點下widget1按鈕,會將 "from Main" 帶到 Second畫面

Second畫面 :

點上一頁按鈕,會回傳 "from widget1"到Main
如果不是點上一頁按鈕兒是透過其他方法回到前一頁,在Main會顯示 "無參數"

startActivityForResult()除了傳值之外,還會回傳一個requestCode
所以Main要加入onActivityResult()方法,來接收辨別是哪個Activity回傳的requestCode


參數一定是從A-->B , B-->A AB兩個Activity在溝通
傳回參數的時候,只會接收參數,畫面不會刷新成預設畫面


最後要記得在AndroidManifest.xml 註冊 Activity
  1. <activity android:name=".SecondActivityName"></activity>


public class MainActivity extends AppCompatActivity {
private Button widget1;
private int REQUEST_CODE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
widget1 =(Button)findViewById(R.id.btnNext);
//下一頁
widget1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.setClass(MainActivity.this, widgetActivity.class);
intent1.putExtra("activityMain", "from Main");
//當 REQUEST_CODE =-1 等於執行 startActivity
// widgetActivity 回傳資料要使用 setResult
startActivityForResult(intent1,REQUEST_CODE);
//startActivity(intent1);
//MainActivity.this.finish();
}
});
}
//接收回傳值的 Function
@Override
protected void onActivityResult(int requestCode , int resultCode,Intent data)
{
if(requestCode == REQUEST_CODE)
{
if (resultCode == RESULT_OK){
String temp=null;
Bundle extras = data.getExtras();
if(extras != null)
{
temp = extras.getString("widget");
}
setTitle(temp);
}else
{
setTitle("無參數");
}
}
}
}
public class widgetActivity extends AppCompatActivity {
private Button prev;
private String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget);
prev=(Button)findViewById(R.id.btnPrev);
Bundle extras =getIntent().getExtras();
if(extras !=null)
{
data=extras.getString("activityMain");
setTitle("widget :" + data);
}else
{
setTitle("widget");
}
prev.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
Intent intent =new Intent();
intent.setClass(widgetActivity.this,MainActivity.class);
// Bundle 傳遞 Data
Bundle bundle = new Bundle();
bundle.putString("widget","from widget1");
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
});
}

2015年8月5日 星期三

[Android]初學-控制項應用



  1. 按下按鈕,顯示EditView輸入的內容和選到的CheckBox
  2. 顯示框出現,利用RadioGroup選擇Show出訊息或隱藏訊息
  3. 按下清除按鈕,EditView、CheckBox 回復初始狀態

public class MainActivity extends AppCompatActivity {
private Button press;
private Button clear;
private TextView Name;
private TextView msg;
private ArrayList<CheckBox> cbList = new ArrayList<CheckBox>();
private RadioGroup rGroup;
private RadioButton btnShow;
private RadioButton btnHide;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
SetDefault();
//msg內容
press.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
String r="";
//第一種CheckBox批次跑迴圈寫法
int ckID[]={R.id.checkBox1,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,R.id.checkBox6};
CheckBox a =null;
for(int i=0 ;i<ckID.length;i++)
{
if((a=(CheckBox)findViewById(ckID[i]))!=null)
{
if(a.isChecked())
{
r+=a.getText()+",";
}
}
}
setTitle("MyApp1 Name:" + Name.getText());
msg.setText("checked: " + r.substring(0,r.length()-1) );
rGroup.setVisibility(View.VISIBLE);
}
});
//控制 msg可見
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == btnHide.getId()) {
msg.setVisibility(View.GONE);
} else if (checkedId == btnShow.getId()) {
msg.setVisibility(View.VISIBLE);
}
}
});
}
private void findView(){
msg=(TextView)findViewById(R.id.textView4);
press = (Button)findViewById(R.id.button1);
clear=(Button)findViewById(R.id.btnclear);
Name=(TextView)findViewById(R.id.editText);
rGroup=(RadioGroup)findViewById(R.id.rGroup);
btnShow =(RadioButton)findViewById(R.id.radioButton1);
btnHide =(RadioButton)findViewById(R.id.radioButton2);
CheckBox ck1 =(CheckBox)findViewById(R.id.checkBox1);
CheckBox ck2 =(CheckBox)findViewById(R.id.checkBox2);
CheckBox ck3 =(CheckBox)findViewById(R.id.checkBox3);
CheckBox ck4 =(CheckBox)findViewById(R.id.checkBox4);
CheckBox ck5 =(CheckBox)findViewById(R.id.checkBox5);
CheckBox ck6 =(CheckBox)findViewById(R.id.checkBox6);
cbList.add(ck1);
cbList.add(ck2);
cbList.add(ck3);
cbList.add(ck4);
cbList.add(ck5);
cbList.add(ck6);
}
//清除
private void SetDefault(){
clear.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v)
{
//第二種CheckBox批次跑迴圈寫法
//批次取消CheckBox選項
for (CheckBox c: cbList) {
c.setChecked(false);
}
msg.setText("Click Clear");
Name.setText("");
setTitle("MyApp1");
msg.setVisibility(View.VISIBLE);
rGroup.setVisibility(View.GONE);
}
});
}
//msg可見
private void SetMsgVisiable(){
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
view raw .java hosted with ❤ by GitHub
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoText="false"
android:text="@string/hello_world"
/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_marginTop="29dp"
android:hint="請輸入姓名"
android:layout_below="@+id/txt1"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帳號"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignBaseline="@+id/editText"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="寫程式"
android:textSize="@dimen/abc_text_size_small_material"
android:layout_below="@+id/editText"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/checkBox1"
android:layout_marginStart="23dp"
android:layout_toEndOf="@+id/checkBox1"
android:checked="false"
android:text="聽音樂" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkBox2"
android:layout_marginStart="30dp"
android:layout_toEndOf="@+id/checkBox2"
android:checked="false"
android:text="看書" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯示"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除"
android:id="@+id/btnclear"
android:layout_below="@+id/button1"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#405dff"
android:layout_below="@+id/rGroup"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnclear"
android:layout_alignParentStart="true"
android:id="@+id/rGroup"
android:visibility="invisible">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show msg"
android:id="@+id/radioButton1"
android:checked="true"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide msg"
android:id="@+id/radioButton2"
android:layout_below="@+id/btnclear"
android:layout_alignParentStart="true"
android:checked="false"
/>
</RadioGroup>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="運動 "
android:id="@+id/checkBox4"
android:layout_below="@+id/checkBox1"
android:layout_alignParentStart="true"
android:checked="false" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡覺"
android:id="@+id/checkBox5"
android:checked="false"
android:layout_below="@+id/checkBox2"
android:layout_alignStart="@+id/checkBox2" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看電影"
android:id="@+id/checkBox6"
android:checked="false"
android:layout_below="@+id/checkBox3"
android:layout_toEndOf="@+id/checkBox5"
android:layout_alignStart="@+id/checkBox3" />
</RelativeLayout>
view raw .xml hosted with ❤ by GitHub





2015年8月3日 星期一

[Android] 基本Layout屬性



版面方向

android:orientation vertical :縱向 版面由上到下horizontal:橫向

寬高設定

android:layout_width
android:layout_height

fill_parent:寬或高滿版
wrap_content:寬或高自動調整,顯示完整內容
match_parent:同fill_parent

設定View的相對位置

android:layout_gravity

設定View內容的位置

android:gravity




2015年7月23日 星期四

[Javascript]頁面刷新、導頁的常見做法



  • 刷新現在所在頁面,相當於Client端按下F5
           location.reload()

           history.go(0)
  •  若要回到上一頁 可以寫成
           history.go(-1)
  •  回到上一頁並刷新 
           history.back()
  •  跳轉到指定頁面 
          location.href=url

[AngularJS]初學 ng-show 和 ng-hide


ng-show 和 ng-hide  沒有指定初始值 , 值為false

所以 ng-show=false 被隱藏

         ng-false=false 顯示


應用:
  1. 可以利用變數來控制True , False
  2. 可以使用ng-model 綁定select checkbox 等等


[AngularJS]初學 練習


這篇是看 男丁格爾 的教學,練習的產物

在藍色框框點兩下,可以進入編輯模式,編輯音樂家的譯名、原名和年齡

在叉叉的地方點兩下,可以把那筆資料砍掉 (●’ω`●)

有空再來貼 和資料庫互動的CODE 唄~~ ( ̄︶ ̄)





2015年7月22日 星期三

[AngularJS] ng-click 的用法

要寫angularJS 除了要引用它的JS外,還必須要利用ng-app 初始化這個範圍的程式

EX : <html ng-app> , <body ng-app> , <div ng-app>

也可以在初始化時,指定模組(Model)

EX : <body ng-app="modelTest"> , <div ng-app="modelTest">

接著要用 ng-controller 指定controller , 這裡宣告為 MsgController


ng-init : 設定初始值

2015年5月25日 星期一

高雄三天兩夜 捷運趴趴造

Day 1

5/29 時間 行程 備註
19:00 搭高鐵 前往左營
21:00 抵達左營 前往凹子底
21:40 瑞豐夜市 吃晚餐 巨蛋站

Day 2

5/30 時間 行程 備註
9:00 早餐
10:30 義大OutLay
  • 8501 左營站上車 新光三越1號出口3號月台
  • 8502 凹子底捷運站 住商部動產前搭車 4:10 5:30 6:40
18:00 義大-->回程
  • 8502 義大購物廣場C區大門口 ---> 凹子底捷運站 4:10 5:30 6:40
  • 8503 高雄文化中心下車 4:20 5:30 6:20
  • 義大接駁車資訊
19:00 前往中央公園-城市光廊 痞子英雄拍攝場景
19:30 前往愛河 吃晚餐 尋找丹丹
21:00 愛河之心 看夜景 居酒屋 看夜景

Day 3

5/31 時間 行程 備註
9:30 早餐 可以去凹子底森林公園逛逛
11:00 高美館 在凹子底搭紅35a公車
12:30 民和茶屋/上林茶屋
14:00 三多商圈/火車站逛逛
16:15 前往左營 回台北 5點發車


行程地圖


2015年5月11日 星期一

[javascript] replace全部的指定字元

javascript 的replace 只能替換掉第一個找到的字元

  1. var str ="Hello Guest , Guest Hello !" ;
  2. alert(str.replace("Hello" , "Hi"));


如果想要整串字串的指定字元被替換掉, 就要用正規寫法(RegExp)
來完成


  1. var str ="Hello Guest , Guest Hello !" ;
  2. alert(str.replace(/Hello/g , "Hi"));
  3.  
  4. var str2 = "AA \ BB \ CC";
  5. var new =str2.replace(/\\/g , "_");
  6. alert(new);

2015年4月14日 星期二

申請Facebook app ID


最近一直在搞 FaceBook,本來和我很不熟的FB ,瞬間看他 超 不 順 眼 的 (不~~)

剛好有空(卡)閒(關)時間,記一下申請App ID 的方法

至於設定 有空再來弄XD

(雖然不知道何月何日FB又要來更新一下)

1. 先進到Facebook Developers 頁面 https://developers.facebook.com/apps

2.點擊註冊 然後在註冊按鈕的位置 會出現一個Create a New App 的按鈕

   一樣點下去

3.會跳出視窗 填好 App名稱、命名空間和分類 按Create App ID

   田下驗證碼就OK了 ~~

  命名空間只能填小寫英文 不然會出錯(下面的圖是錯誤示範XD)











2015年4月13日 星期一

FB Tag好友 發分享文到塗鴉牆


先說說前置作業,這功能一共要向Facebook申請兩個權限

一個是Taggable Friends ,另一個是 publish_actions



要使用這個功能,必須要先到 Status & Review 頁面,提交APP 供FB審核通過,才能使用

審核的時間最長約7天,需用英文說明你的APP並截圖,才會過關

詳細設定方式請參考 此篇文章 

 申請教學 https://www.ptt.cc/bbs/Web_Design/M.1408956880.A.98D.html

實作範例程式碼 我是參考 這篇

查了些資料 希望會成功 !!!

附上英文參考資料 :https://github.com/sauce/guide/wiki/Facebook%27s-approval-process#publish-actions

2015/4/14 終於有頭緒了 不枉費今天身體不適 努力打起精神瘋狂GOOGLE  T__T