不能重複的Toast

 


 

 

public class MainActivity extends Activity {
private Button button_Toast, button_Toast_No;
private Toast toast;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();

}

public void Click(View v) {
switch (v.getId()) {
// 不能連續按的Toast:
case R.id.button1:
if (toast == null) {
toast = Toast.makeText(getApplicationContext(), "你在按啊!!",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, -100);
// 設定toast出現的位置(左邊越小-,右邊越大+,上面越小-,0,0代表正中間);
} else {
toast.setText("你在按啊!!");
}
toast.show();
break;

// 普通Toast:
case R.id.button2:
Toast.makeText(getApplicationContext(), "我會一直重複...",
Toast.LENGTH_SHORT).show();

}
}

public void findView() {

button_Toast_No = (Button) this.findViewById(R.id.button1);
button_Toast = (Button) this.findViewById(R.id.button2);
}

}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 小Chiu 的頭像
    小Chiu

    Cuiu's Home

    小Chiu 發表在 痞客邦 留言(0) 人氣()