1.MainActivity.java
package com.example.alertdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:startActivity(new Intent(MainActivity.this,SimpleComponentActivity.class));
break;
case R.id.button2:
startActivity(new Intent(this,MenuActivity.class));
break;
case R.id.button3:
startActivity(new Intent(this,ProgressActivity.class));
break;
case R.id.button4:startActivity(new Intent(MainActivity.this,DialogActivity.class));
}
}
}
2.MenuActivity.java
package com.example.alertdialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;
public class MenuActivity extends Activity {
private Button btn_test2_show_cm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
btn_test2_show_cm=(Button) findViewById(R.id.btn_test2_show_cm);
btn_test2_show_cm.setOnCreateContextMenuListener(this);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
//添加菜单项
menu.add(0, 1, 0, "添加");
menu.add(0, 4, 0, "删除");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 2, 0, "添加");
menu.add(0, 3, 0, "删除");/*
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.option_menu, menu);*/
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "添加", 0).show();
break;
case 4:
Toast.makeText(this, "删除", 0).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 2:
Toast.makeText(this, "添加", 0).show();
break;
case 3:
Toast.makeText(this, "删除", 0).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
3,ProgressActivity.java
package com.example.alertdialog;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class ProgressActivity extends Activity implements OnSeekBarChangeListener {
private LinearLayout ll_progress_loading;
private ProgressBar pb_progress_loading;
private SeekBar sb_progress_loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
ll_progress_loading=(LinearLayout) findViewById(R.id.ll_progress_loading);
pb_progress_loading=(ProgressBar) findViewById(R.id.pb_progress_loading);
sb_progress_loading=(SeekBar) findViewById(R.id.sb_progress_loading);
sb_progress_loading.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
Log.e("tag", "离开滑杆");
int progress2 = sb_progress_loading.getProgress();
pb_progress_loading.setProgress(progress2);
if(progress2==pb_progress_loading.getMax()){
ll_progress_loading.setVisibility(View.INVISIBLE);
}else{
ll_progress_loading.setVisibility(View.VISIBLE);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.e("tag", "按下滑杆");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.e("tag", "滑杆移动");
}
}
4.SimpleComponentActivity.java
package com.example.alertdialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class SimpleComponentActivity extends Activity {
private TextView tv;
private EditText et_simple_number;
private Button btn_simple_submit;
private boolean flag;
private CheckBox cb_simple_basket;
private CheckBox cb_simple_foot;
private CheckBox cb_simple_pingpang;
private RadioGroup rg_simple_sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_component);
tv=(TextView) findViewById(R.id.tv_simple_message);
tv.setText("啦啦啦~~~");
et_simple_number=(EditText) findViewById(R.id.et_simple_number);
btn_simple_submit=(Button) findViewById(R.id.btn_simple_submit);
flag=true;
btn_simple_submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String number=et_simple_number.getText().toString();
Toast.makeText(SimpleComponentActivity.this, number, 0).show();
}
});
final ImageView imageView=(ImageView) findViewById(R.id.iv_simple_play);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
imageView.setBackgroundResource(android.R.drawable.alert_light_frame);
imageView.setImageResource(android.R.drawable.ic_media_pause);
}else{
imageView.setBackgroundResource(android.R.drawable.alert_dark_frame);
imageView.setImageResource(android.R.drawable.ic_media_play);
}
flag=!flag;
}
});
rg_simple_sex=(RadioGroup) findViewById(R.id.rg_simple_sex);
rg_simple_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb=(RadioButton) findViewById(checkedId);
String sex=rb.getText().toString();
Toast.makeText(SimpleComponentActivity.this, sex, 0).show();
}
});
cb_simple_basket=(CheckBox) findViewById(R.id.cb_simple_basket);
cb_simple_foot=(CheckBox) findViewById(R.id.cb_simple_foot);
cb_simple_pingpang=(CheckBox) findViewById(R.id.cb_simple_pingpang);
cb_simple_pingpang.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(SimpleComponentActivity.this, "选中了乒乓", 0).show();
}else{
Toast.makeText(SimpleComponentActivity.this, "取消选中乒乓", 0).show();
}
}
});
}
public void confirm(View v){
StringBuffer sb=new StringBuffer();
if(cb_simple_basket.isChecked()){
sb.append(cb_simple_basket.getText().toString()).append(" ");
}
if(cb_simple_foot.isChecked()){
sb.append(cb_simple_foot.getText().toString()).append(" ");
}
if(cb_simple_pingpang.isChecked()){
sb.append(cb_simple_pingpang.getText().toString());
}
Toast.makeText(this, sb.toString(), 0).show();
}
}
5.DialogActivity.javapackage com.example.alertdialog;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.Toast;
public class DialogActivity extends Activity {
private Thread thread;
private LinearLayout vie;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
vie=(LinearLayout) findViewById(R.id.lin);
}
public void showAD(View v){
new AlertDialog.Builder(this).setTitle("删除数据")
.setMessage("你确定删除数据嘛??")
.setPositiveButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(DialogActivity.this, "删除数据", 0).show();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(DialogActivity.this, "取消删除数据", 0).show();
}
}).show();
}
public void showLD(View v){
final String[] items={"红","蓝","绿","灰"};
new AlertDialog.Builder(this).setTitle("指定背景颜色").setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//Toast.makeText(DialogActivity.this, items[which], 0).show();
switch(which){
case 0:vie.setBackgroundColor(0x77ff0000);
break;
case 1:vie.setBackgroundColor(0x770000ff);
break;
case 2:vie.setBackgroundColor(0x7700ff00);
break;
case 3:vie.setBackgroundColor(0x77ababab);
break;
default:break;
}
dialog.dismiss();
}
}).show();
}
public void showPD(View v){
final ProgressDialog dialog=ProgressDialog.show(this, "数据加载", "数据加载中。。。");
new Thread(){
public void run(){
for(int i=0;i<20;i++){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
dialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(DialogActivity.this, "加载完成了!!!", 0).show();
}
});
}
}.start();
}
public void showPD2(View v){
final ProgressDialog pd=new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
new Thread(new Runnable() {
@Override
public void run() {
int count=20;
pd.setMax(count);
for(int i=0;i<20;i++){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pd.setProgress(pd.getProgress()+1);
}
pd.dismiss();
}
}).start();
}
public void showCD(View v){
View view=View.inflate(this, R.layout.dialog_view, null);
final EditText nameET=(EditText) view.findViewById(R.id.et_dialog_name);
final EditText pwdET=(EditText) view.findViewById(R.id.et_dialog_pwd);
new AlertDialog.Builder(this)
.setNegativeButton("取消", null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String username=nameET.getText().toString();
String pwd=pwdET.getText().toString();
Toast.makeText(DialogActivity.this, username+": "+pwd, 0).show();
}
})
.setView(view).show();
}
public void showDateAD(View v){
Calendar calendar=Calendar.getInstance();
final int year=calendar.get(Calendar.YEAR);
final int monthOfYear=calendar.get(Calendar.MONTH);
final int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
Log.e("TAG",year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Log.e("TAG",year+"--"+monthOfYear+"--"+dayOfMonth);
}
}, year, monthOfYear, dayOfMonth).show();
}
public void showTimeAD(View v){
Calendar calendar=Calendar.getInstance();
int hourOfDay=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
Log.e("TAG",hourOfDay+":"+minute);
new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
Log.e("TAG",hourOfDay+"::"+minute);
}
}, hourOfDay, minute, true).show();
}
}
6.activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试常用菜单Component" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试菜单Component" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试进度条Component" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试对话框Component" />
</LinearLayout>
7.activity_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/lin" >
<Button
android:id="@+id/bt1"
android:onClick="showAD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示一般Dialog" />
<Button
android:id="@+id/bt2"
android:onClick="showLD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示单选列表AlertDialog" />
<Button
android:id="@+id/bt3"
android:onClick="showCD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示自定义AlertDialog" />
<Button
android:id="@+id/bt4"
android:onClick="showPD"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:text="显示圆形进度ProgressDialog" />
<Button
android:onClick="showPD2"
android:id="@+id/bt5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示水平进度ProgressDialog" />
<Button
android:layout_marginTop="20dp"
android:id="@+id/bt6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示DataPickerDialog"
android:onClick="showDateAD" />
<Button
android:id="@+id/bt7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示TimePickDialog"
android:onClick="showTimeAD"/>
</LinearLayout>
8.activity_menu.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_test2_show_cm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示ContextMenu" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="1.点击menu显示选项菜单\n2,长按按钮显示上下文菜单"
android:textSize="25sp" />
</LinearLayout>
9.activity_progress.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll_progress_loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载中。。。" />
</LinearLayout>
<ProgressBar
android:id="@+id/pb_progress_loading"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="30" />
<SeekBar
android:id="@+id/sb_progress_loading"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1.滑动下面的滑杆后,上面的进度条会同步\n2.滑动到最大值时,最上面的进度条会消失" />
</LinearLayout>
10.activity_simple_component.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_simple_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999999"
android:text="这是TextView的内容"
android:textColor="#ff0000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_simple_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号"
android:inputType="phone" />
<Button
android:id="@+id/btn_simple_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提 交" />
<ImageView
android:id="@+id/iv_simple_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/alert_dark_frame"
android:src="@android:drawable/ic_media_play" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:" />
<CheckBox
android:id="@+id/cb_simple_basket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" />
<CheckBox
android:id="@+id/cb_simple_foot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />
<CheckBox
android:id="@+id/cb_simple_pingpang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="confirm"
android:text="确定" />
</LinearLayout>
<RadioGroup
android:id="@+id/rg_simple_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_simple_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/rb_simple_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:checked="true" />
<RadioButton
android:id="@+id/rb_simple_nofale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人妖" />
</RadioGroup>
</LinearLayout>
11.dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/ppp" />
<EditText
android:id="@+id/et_dialog_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/et_dialog_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
android:ems="10" />
</LinearLayout>