真希望天天都是星期天之 AlertDialog & CheckBox
星期天不是錯啦!不過好像星期六更好,因為明天還是繼續放假,來吧!利用AlertDialog弄個一週的CheckBox清單吧,把想放假的那幾天勾起來。
1. 建立專案
2. 加個TextView & Button
3. Source Code
重點就是String[] & boolean[]最好一樣,如果不一樣,嘿嘿,這麼簡單還要教,自己試試看。
package
com.example.ex003;
import
android.os.Bundle;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import android.view.Menu;
import
android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
public class MainActivity extends Activity {
private String[] week = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
private boolean[] bweek = {false,false,false,false,false,false,false};
private TextView tv1;
private Button btn1;
private String repeat = "";
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)
MainActivity.this.findViewById(R.id.textView1);
btn1 = (Button)
MainActivity.this.findViewById(R.id.button1);
btn1.setOnClickListener(new
Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
repeat = "";
new
AlertDialog.Builder(MainActivity.this)
.setTitle("EX003")
.setMultiChoiceItems(
week,
bweek,
new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
}
})
.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
for (int i = 0; i < bweek.length; i++) {
if (bweek[i]) {
repeat = repeat + week[i];
}
}
tv1.setText(repeat);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
});
}
}
|
4. 就是這個單,就是這個單
5. 搞定了,收工嗎?
6. 專業專業,菜鳥新上手,當然給老闆要有物超所值的fu,若全勾了,就來個每天
7. Source Code
package
com.example.ex003;
import
android.os.Bundle;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import android.view.Menu;
import
android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
public class MainActivity extends Activity {
private String[] week = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
private boolean[] bweek = {false,false,false,false,false,false,false};
private TextView tv1;
private Button btn1;
private String repeat = "";
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)
MainActivity.this.findViewById(R.id.textView1);
btn1 = (Button)
MainActivity.this.findViewById(R.id.button1);
btn1.setOnClickListener(new
Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
repeat = "";
new
AlertDialog.Builder(MainActivity.this)
.setTitle("EX003")
.setMultiChoiceItems(
week,
bweek,
new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
}
})
.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// for
(int i = 0; i < bweek.length; i++) {
// if
(bweek[i]) {
// repeat
= repeat + week[i];
// }
// }
chkday();
tv1.setText(repeat);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
});
}
private void chkday(){
int everyday = 0;
for (int i = 0; i < bweek.length; i++) {
if (bweek[i]) {
repeat = repeat + week[i];
everyday++;
}
}
if (everyday == 7)
repeat = "每日";
}
}
|
這是我的一小步,也是人類的一小步,不過多寫一點,以後就可以剪下貼上,然後早點回家...............................................,嗯,吃完早餐再回家,老闆 來個燒餅油條加豆漿!!!
標籤: AlertDialog, CheckBox
0 個意見:
張貼留言
<< 首頁