2016年6月22日 星期三

Android imageButton底圖透明

在XML中將imageButton的屬性中增加android:background="@android:color/transparent",就可以將imageButton底圖設為透明

2015年12月3日 星期四

Android常用監聽

Button
View.OnClickListener
物件點一下

View.OnLongClickListener
物件長按

Radio
RadioGroup.OnCheckedChangeListener
單選群組中,選項變更

EditText
TextWatcher
輸入文字時

CheckBox
CompoundButton.OnCheckedChangeListener
複選選項變更


Spinner
AdapterView.OnitemSelectedListener
一選取項目就觸發






Android要使用Listener來監聽物件

//首先需在MainActivity implements監聽動作
public class MainActivity extends AppCompatActivity
implements RadioGroup.OnCheckedChangeListener,TextWatcher
{
RadioGroup unit;
EditText value;
TextView txv;

@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
//設定要監聽的物件
    unit=(RadioGroup)findViewById(R.id.radioGroup);
    unit.setOnCheckedChangeListener(this);
    value=(EditText)findViewById(R.id.editText);
    value.addTextChangedListener(this);
    txv=(TextView)findViewById(R.id.textView2);

}

//以下是監聽的事件
@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {
    calc();
}


@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Overridepublic void afterTextChanged(Editable s) {
    calc();
}

protected void calc(){
   
}

}

使用String.format()格式化字串

String.format()可以用來格式化字串

例如
浮點數取小數點後一位
String.format("%.1f",3.14159);會取得3.1

%將3.14159帶入
.1取得小數點後一位
f要轉換的值是浮點數

判斷EditText有無輸入值

要判斷EditText有無輸入值,可用以下方式判斷
if("".equals(editText.getText().toString().trim()))
{
}

或可用
editText.getText().length()== 0

editText.getText().toString() == null 
editText.getText().toString().equals("")

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>