主界面布局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/btn_main_test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试常用简单的Component" />
<Button
android:id="@+id/btn_main_test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试菜单Component" />
<Button
android:id="@+id/btn_main_test3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试进度条Component" />
<Button
android:id="@+id/btn_main_test4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试对话框Component" />
</LinearLayout>
点不同的按钮,启动不同的Activity
package com.example.uidemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_main_test1).setOnClickListener(this);
findViewById(R.id.btn_main_test2).setOnClickListener(this);
findViewById(R.id.btn_main_test3).setOnClickListener(this);
findViewById(R.id.btn_main_test4).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_main_test1:
startActivity(new Intent(this, SimpleActivity.class));
break;
case R.id.btn_main_test2:
startActivity(new Intent(this, MenuActivity.class));
break;
case R.id.btn_main_test3:
startActivity(new Intent(this, ProgressActivity.class));
break;
case R.id.btn_main_test4:
startActivity(new Intent(this, DialogActivity.class));
break;
default:
break;
}
}
}
简单的组件
activity_simple.xml
<LinearLayout 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: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" >
</EditText>
<Button
android:id="@+id/btn_simple_submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提交" />
<ImageView
android:id="@+id/iv_simple_icon"
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: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: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:text="男" />
<RadioButton
android:id="@+id/rb_simple_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="女" />
<RadioButton
android:id="@+id/rb_simple_nomale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="东方不败" />
</RadioGroup>
</LinearLayout>
SimpleActivity.java
package com.example.uidemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
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 SimpleActivity extends Activity {
private TextView tv_simple_message;
private EditText et_simple_number;
private Button btn_simple_submit;
private ImageView iv_simple_icon;
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);
tv_simple_message = (TextView) findViewById(R.id.tv_simple_message);
tv_simple_message.setText("huangbaokang");
et_simple_number = (EditText) findViewById(R.id.et_simple_number);
btn_simple_submit = (Button) findViewById(R.id.btn_simple_submit);
btn_simple_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number = et_simple_number.getText().toString();
Toast.makeText(SimpleActivity.this, number, 0).show();
}
});
iv_simple_icon = (ImageView) findViewById(R.id.iv_simple_icon);
iv_simple_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv_simple_icon
.setBackgroundResource(android.R.drawable.alert_light_frame);
iv_simple_icon
.setImageResource(android.R.drawable.ic_media_pause);
}
});
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_foot
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(SimpleActivity.this, "选中了足球", 0)
.show();
} else {
Toast.makeText(SimpleActivity.this, "未选中足球", 0)
.show();
}
}
});
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 radioButton = (RadioButton) findViewById(checkedId);
// 得到文本
String sex = radioButton.getText().toString();
// 提示
Toast.makeText(SimpleActivity.this, sex, 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();
}
}
菜单UI
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="1. 点击menu显示选项菜单\n2. 长按按钮显示上下文菜单"
android:textSize="25dp" />
</LinearLayout>
处理Activity
MenuActivity.java
package com.example.uidemo;
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;
/*
OptionMenu
1. 如何触发Menu的显示?
点击menu键
2. 如何向Menu中添加MenuItem?
重写onCreateOptionMenu()
menu.add()或者加载菜单文件
3. 选择某个MenuItem时如何响应?
重写onOptionsItemSelected(), 根据itemId做响应
ContextMenu
1. 如何触发Menu的显示?
长按某个视图
view.setOnCreateContextMenuListener(this)
2. 如何向Menu中添加MenuItem?
重写onCreateContextMenu()
menu.add()
3. 选择某个MenuItem时如何响应?
重写onContextItemSelected(), 根据itemId做响应
*/
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 boolean onCreateOptionsMenu(Menu menu) {
// //add(int groupId, int itemId, int order, CharSequence title)
// // groupId不需要分组传0 order不要求排序的话,传0
// menu.add(0, 100, 0, "添加");
// menu.add(0, 200, 0, "删除");
// return super.onCreateOptionsMenu(menu);
// }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.mymenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
//case 100:
Toast.makeText(this, "添加", 0).show();
break;
case R.id.delete:
//case 200:
Toast.makeText(this, "删除", 0).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, 111, 0, "查看详情");
menu.add(0, 222, 0, "更新数据");
super.onCreateContextMenu(menu, v, menuInfo);
}
// @Override 这个方法要跟下面的onContextItemSelected区别,两个同时放开,我测试会弹窗两次!!!
// public boolean onMenuItemSelected(int featureId, MenuItem item) {
// switch (item.getItemId()) {
// case 111:
// Toast.makeText(this, "查看详情", 0).show();
// break;
// case 222:
// Toast.makeText(this, "更新数据", 0).show();
// break;
// default:
// break;
// }
// return super.onMenuItemSelected(featureId, item);
// }
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 111:
Toast.makeText(this, "查看详情111", 0).show();
break;
case 222:
Toast.makeText(this, "更新数据111", 0).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
}
实现了长按和点击菜单键
定义的菜单item文件 mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/add" android:title="添加"></item>
<item android:id="@+id/delete" android:title="删除"></item>
</menu>
进度条(圆形,水平)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:id="@+id/textView1"
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="fill_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="fill_parent"
android:layout_height="wrap_content"
android:text="1. 滑动下面的滑杆后, 上面的进度条会同步\n2. 滑动到最大值时, 最上面的进度条消失" />
</LinearLayout>
处理Activity类
package com.example.uidemo;
import android.app.Activity;
import android.os.Bundle;
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 {
private SeekBar sb_progress_loading;
private LinearLayout ll_progress_loading;
private ProgressBar pb_progress_loading;
private OnSeekBarChangeListener onSeekBarChangeListener = new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = sb_progress_loading.getProgress();
pb_progress_loading.setProgress(progress);
if(progress==sb_progress_loading.getMax()){
//ll_progress_loading.setVisibility(View.INVISIBLE);// 不可见,但占空间
ll_progress_loading.setVisibility(View.GONE);// 不可见,不占空间
}else{
ll_progress_loading.setVisibility(View.VISIBLE);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
sb_progress_loading = (SeekBar) findViewById(R.id.sb_progress_loading);
ll_progress_loading = (LinearLayout) findViewById(R.id.ll_progress_loading);
pb_progress_loading = (ProgressBar) findViewById(R.id.pb_progress_loading);
sb_progress_loading.setOnSeekBarChangeListener(onSeekBarChangeListener );
}
}
对话框
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" >
<!-- AlertDialog -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAD"
android:text="显示一般AlertDialog" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showLD"
android:text="显示单选列表AlertDialog" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showCD"
android:text="显示自定义AlertDialog" />
<!-- ProgressDialog -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showPD"
android:text="显示圆形进度ProgressDialog"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showPD2"
android:text="显示水平进度ProgressDialog" />
<!-- DatePickerDialog与TimePickerDialog -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showDateAD"
android:text="显示DatePickerDialog"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showTimeAD"
android:text="显示TimePickerDialog" />
</LinearLayout>
处理类
DialogActivity.java
package com.example.uidemo;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
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.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
// 显示一般AlertDialog
public void showAD(View v) {
new AlertDialog.Builder(this).setTitle("删除数据").setMessage("你确定要删除数据吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "删除数据", 0).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "取消删除数据", 0).show();
}
}).show();
}
// 显示单选列表AlertDialog
public void showLD(View v) {
final String[] items = { "红", "绿", "蓝" };
new AlertDialog.Builder(this)
.setTitle("删除数据")
.setSingleChoiceItems(items, 1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(DialogActivity.this,
items[which], 0).show();
dialog.dismiss();// 关闭弹窗
}
}).show();
}
// 显示自定义AlertDialog
public void showCD(View v) {
View view = View.inflate(this, R.layout.dialog_view, null);
final EditText userET = (EditText) view.findViewById(R.id.et_username);
final EditText pwdET = (EditText) view.findViewById(R.id.et_password);
new AlertDialog.Builder(this).setView(view)
.setNegativeButton("取消", null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String username = userET.getText().toString();
String password = pwdET.getText().toString();
// 提示输入项的值
Toast.makeText(DialogActivity.this,
username + " : " + password, 0).show();
dialog.dismiss();
}
}).show();
}
// 显示圆形进度ProgressDialog
public void showPD(View v) {
final ProgressDialog progressDialog = ProgressDialog.show(this, "数据加载",
"数据加载中....");
// 长时间的工作不能在主线程做, 得启动分线程完成
new Thread() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
progressDialog.dismiss(); // 方法在分线程执行, 但内部使用Handler实现主线程移除dialog
// Toast.makeText(DialogActivity.this, "加载完成了!!!", 0).show();
// 更新UI,不能直接使用Toast,上面执行程序会报错,因为不能在子线程更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {// 在主线程执行
Toast.makeText(DialogActivity.this, "加载完成了!!!", 0)
.show();
}
});
};
}.start();
}
// 显示水平进度ProgressDialog
public void showPD2(View v) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("huangbaokang");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
// 4. 启动分线程, 加载数据, 并显示进度, 当加载完成移除dilaog
new Thread(new Runnable() {
@Override
public void run() {
int count = 20;
// 设置最大进度
progressDialog.setMax(count);
for (int i = 0; i < count; i++) {
// 休息一会
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressDialog.setProgress(progressDialog.getProgress() + 1);
}
// 移除dialog
progressDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {// 在主线程执行
Toast.makeText(DialogActivity.this, "加载完成了!!!", 0)
.show();
}
});
}
}).start();
}
public void showDateAD(View v) {
//创建日历对象
Calendar calendar = Calendar.getInstance();
//得到当前的年月日
int year = calendar.get(Calendar.YEAR);//得到年份
int monthOfYear = calendar.get(Calendar.MONTH);//月
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);//得到日
Log.e("TAG", year+"-"+monthOfYear+"-"+dayOfMonth);
new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Log.e("TAG", year+"--"+(monthOfYear+1)+"--"+dayOfMonth);
}
}, year, monthOfYear, dayOfMonth).show();
}
public void showTimeAD(View v) {
Calendar c = Calendar.getInstance();
int hourOfDay = c.get(Calendar.HOUR_OF_DAY); //得到小时
int minute = c.get(Calendar.MINUTE); //得到分钟
Log.e("TAG", hourOfDay+":"+minute);
new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Log.e("TAG", hourOfDay+"::"+minute);
}
}, hourOfDay, minute, true).show();
}
}
一般AlertDialog
显示单选列表AlertDialog
显示自定义AlertDialog
圆形进度条ProgressDialog
水平进度条ProgressDialog
时间对话框DatePickerDialog
日期对话框TimePickerDialog
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.uidemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SimpleActivity"
android:label="@string/title_activity_simple" >
</activity>
<activity
android:name=".MenuActivity"
android:label="@string/title_activity_menu" >
</activity>
<activity
android:name=".ProgressActivity"
android:label="@string/title_activity_progress" >
</activity>
<activity
android:name=".DialogActivity"
android:label="@string/title_activity_dialog" >
</activity>
</application>
</manifest>