UI的定义

全称user interface, 意为: 用户界面

UI由View和ViewGroup组成

View类是所有视图(包括ViewGroup)的根基类

View在屏幕上占据一片矩形区域, 并会在上面进行内容绘制

ViewGroup包含一些View或ViewGroup, 用于控制子View的布局

Android之UI的定义,组成和事件和各种UI的基本使用例子_android

UI组成

界面的整体布局(layout)
就是很多的ViewGroup和View组成的布局
组成可视界面的各个UI组件(Component)
就是上面的View对应的组件

UI事件

当用户通过手指触摸UI时, 系统会自动创建对应的Event对象
Android中提供了多种方式拦截处理不同类型的事件
那事件是如何被调用的呢?
是Activity调用?不是的,
视图本身就可以处理发生在该视图上的事件,监听器本来就是设置
在这个视图的上面,当发生事件的时候是由视图去调用监听器,

Android之UI的定义,组成和事件和各种UI的基本使用例子_android_02

给视图添加事件监听的方式
​​​view.seton…Listener(listener)​

练习例子

主界面

多个例子的汇总界面
布局文件

<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.hello;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class Component extends Activity implements OnClickListener
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_component);
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, SimpleComponentActivity.class));
break;
case R.id.btn_main_test2:
startActivity(new Intent(this, MenuComponentActivity.class));

break;
case R.id.btn_main_test3:
startActivity(new Intent(this, ProgressComponentActivity.class));

break;
case R.id.btn_main_test4:
startActivity(new Intent(this, DialogComponentActivity.class));
break;

default:
break;
}
}
}

简单的组件

布局文件

<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/SimpleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999999"
android:text="上面的标题文字"
android:textColor="#ff0000"
android:textSize="18dp" />

<EditText
android:id="@+id/SimpleEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入手机号"
android:inputType="phone" >

<requestFocus />
</EditText>

<Button
android:id="@+id/SimpleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提交" />

<ImageView
android:id="@+id/SimpleImageView"
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/SimpleCheckBoxBasket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" />

<CheckBox
android:id="@+id/SimpleCheckBoxFoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />

<CheckBox
android:id="@+id/SimpleCheckBoxPP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓球" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="confirm"/>

</LinearLayout>

<RadioGroup
android:id="@+id/SimpleRadioGroupSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:id="@+id/SimpleRadioGroupMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />

<RadioButton
android:id="@+id/SimpleRadioGroupFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />

<RadioButton
android:id="@+id/SimpleRadioGroupSecret"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保密" />
</RadioGroup>

</LinearLayout>

activity

package com.example.hello;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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
{
TextView SimpleTextView;
EditText SimpleEditText;
Button SimpleButton;
ImageView SimpleImageView;

CheckBox SimpleCheckBoxBasket;
CheckBox SimpleCheckBoxFoot;
CheckBox SimpleCheckBoxPP;

RadioGroup SimpleRadioGroupSex;

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

//TextView
SimpleTextView= (TextView) findViewById(R.id.SimpleTextView);
SimpleTextView.setText("你好");

//EditText
SimpleEditText=(EditText) findViewById(R.id.SimpleEditText);

//Button
SimpleButton=(Button) findViewById(R.id.SimpleButton);
SimpleButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String phone = SimpleEditText.getText().toString();
Toast.makeText(SimpleComponentActivity.this, phone, 0).show();
}
});

//ImageView
SimpleImageView=(ImageView) findViewById(R.id.SimpleImageView);
SimpleImageView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//设置背景图片
SimpleImageView.setBackgroundResource(android.R.drawable.alert_light_frame);
//设置前景图片
SimpleImageView.setImageResource(android.R.drawable.ic_media_pause);
}
});

//CheckBox
SimpleCheckBoxBasket=(CheckBox) findViewById(R.id.SimpleCheckBoxBasket);
SimpleCheckBoxFoot=(CheckBox) findViewById(R.id.SimpleCheckBoxFoot);
SimpleCheckBoxPP=(CheckBox) findViewById(R.id.SimpleCheckBoxPP);

SimpleCheckBoxFoot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
Toast.makeText(SimpleComponentActivity.this, "选了足球", 0).show();
}
else
{
Toast.makeText(SimpleComponentActivity.this, "未选足球", 0).show();
}
}
});

//RadioGroup
SimpleRadioGroupSex=(RadioGroup) findViewById(R.id.SimpleRadioGroupSex);
SimpleRadioGroupSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton radioButton = (RadioButton) findViewById(checkedId);
String sex = radioButton.getText().toString();
Toast.makeText(SimpleComponentActivity.this, sex, 0).show();
}
});

}
public void confirm(View v)
{
StringBuffer sb=new StringBuffer();
if(SimpleCheckBoxBasket.isChecked())
{
sb.append(SimpleCheckBoxBasket.getText().toString()).append(" ");
}
if(SimpleCheckBoxFoot.isChecked())
{
sb.append(SimpleCheckBoxFoot.getText().toString()).append(" ");
}
if(SimpleCheckBoxPP.isChecked())
{
sb.append(SimpleCheckBoxPP.getText().toString()).append(" ");
}
Toast.makeText(this, sb, 0).show();
}
}

menu组件

布局文件

<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/menuButton"
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文件

package com.example.hello;

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 MenuComponentActivity extends Activity
{
/*
* Android的Menu有OptionMenu和ContextMenu两种
* OptionMenu:选择菜单,这个需要按手机的菜单键触发
* ContextMenu:上下文菜单,这个比如微信按住某个组件,会出现菜单给你选择
*
* OptionMenu:
* 如何触发Menu的显示?
OptionMenu在点击手机的menu键触发

如何向Menu中添加MenuItem?
Activity中有回调方法onCreateOptionsMenu(Menu menu)
显示OptionMenu的回调方法, 在此方法中向Menu中添加MenuItem

添加menuItem的两种方式:
纯编码方式: menu.add(….)
加载menu文件的方式:
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_option, menu);


选择某个MenuItem时如何响应?
Activity中又有回调方法onOptionsItemSelected(MenuItem item)
当选择某个菜单项的回调方法


ContextMenu
如何触发Menu的显示?
长按触发
View中setOnCreateContextMenuListener(listener)
为某个视图添加创建ContextMenu的监听(需要长按触发)

如何向Menu中添加MenuItem?
重写onCreateContextMenu()

选择某个MenuItem时如何响应?

*/

Button menuButton;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_component);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
//编码的方式
/* @param groupId The group identifier that this item should be part of.
* This can be used to define groups of items for batch state
* changes. Normally use {@link #NONE} if an item should not be in a
* group.
* 这是分组的id,如果这个item不需要分组,就可以传入none
* @param itemId Unique item ID. Use {@link #NONE} if you do not need a
* unique ID.
* item的标识id
* @param order The order for the item. Use {@link #NONE} if you do not care
* about the order. See {@link MenuItem#getOrder()}.
* item的排序id
* @param title The text to display for the item.
*/
menu.add(0, 1, 0, "添加");
menu.add(0, 2, 0, "删除");

//菜单文件的方式
//1.先在res建好menu的资源
//2.获取menu加载器
MenuInflater menuInflater = getMenuInflater();
//3.用menu加载器加载菜单
menuInflater.inflate(R.menu.mymenu, menu);

//======================================
menuButton=(Button) findViewById(R.id.menuButton);
menuButton.setOnCreateContextMenuListener(this);

return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.add:
Toast.makeText(this, "添加2", 0).show();
break;
case R.id.delete:
Toast.makeText(this, "删除2", 0).show();
break;
case 1:
Toast.makeText(this, "添加", 0).show();
break;
case 2:
Toast.makeText(this, "删除", 0).show();
break;

default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case 3:
Toast.makeText(this, "添加", 0).show();
break;
case 4:
Toast.makeText(this, "删除", 0).show();
break;

default:
break;
}
return super.onContextItemSelected(item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 3, 0, "添加");
menu.add(0, 4, 0, "删除");
}
}

进度条组件

布局文件

<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/ProgressLoading"
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/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="30"/>

<SeekBar
android:id="@+id/seekBar"
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.hello;

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 ProgressComponentActivity extends Activity
{
/*
* Progressbar:进度条
<ProgressBar
android:id="@+id/pb_test3_loading2"
//水平进度条,如果没有设置默认是圆形的进度条
style=“?android:attr/progressBarStyleHorizontal“
android:layout_width="match_parent“
android:layout_height="wrap_content"
android:progress=“2“ //当前进度, 默认为0
android:max=“10”/> // 最大进度, 默认为100

ProgressBar类的方法
void setProgress(int Progress) : 设置当前进度
int getProgress() : 得到当前进度
void setMax(int max) : 设置最大进度
int getMax() : 设置或得到最大进度

View类通用的的方法
void setVisibility(int visibility) : 设置视图的可见性,可以设置的值
View. VISIBLE : 标识可见
View. INVISIBLE : 标识不可见, 但占屏幕空间
View.GONE : 标识不可见, 也不占屏幕空间

SeekBar:可以手动滑动的进度条
SeekBar类里面的方法:
setOnSeekBarChangeListener(OnSeekBarChangeListener l) : 设置改变的监听
OnSeekBarChangeListener里面的方法:
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) : 进度改变
onStartTrackingTouch(SeekBar seekBar) : 按下滑杆
onStopTrackingTouch(SeekBar seekBar) : 从滑杆离开

*/

LinearLayout ProgressLoading;
ProgressBar progressBar;
SeekBar seekBar;
private OnSeekBarChangeListener OnSeekBarChangeListener=new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
//1.得到seekBar进度
int progress = seekBar.getProgress();
//2.设置progressBar的进度条
progressBar.setProgress(progress);
//判断是否达到最大值
if(progress==seekBar.getMax())
{
ProgressLoading.setVisibility(View.INVISIBLE);
}
else
{
ProgressLoading.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_component);

ProgressLoading=(LinearLayout) findViewById(R.id.ProgressLoading);
progressBar=(ProgressBar) findViewById(R.id.progressBar);
seekBar=(SeekBar) findViewById(R.id.seekBar);

//给SeekBar设置监听
seekBar.setOnSeekBarChangeListener(OnSeekBarChangeListener);
}
}

对话框组件

布局文件

<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:id="@+id/btn_test4_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAD"
android:text="显示一般AlertDialog" />

<Button
android:id="@+id/btn_test4_ld"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showLD"
android:text="显示单选列表AlertDialog" />

<Button
android:id="@+id/btn_test4_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showCD"
android:text="显示自定义AlertDialog" />


<!-- ProgressDialog -->
<Button
android:id="@+id/btn_test4_pd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showPD"
android:text="显示圆形进度ProgressDialog"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btn_test4_pd2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showPD2"
android:text="显示水平进度ProgressDialog" />



<!-- DatePickerDialog与TimePickerDialog -->
<Button
android:id="@+id/btn_test4_dd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showDateAD"
android:text="显示DatePickerDialog"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btn_test4_td"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showTimeAD"
android:text="显示TimePickerDialog" />

</LinearLayout>
<?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/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/title"
android:scaleType="fitXY"/>

<EditText
android:id="@+id/et_dialog_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名">
</EditText>

<EditText
android:id="@+id/et_dialog_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"/>

</LinearLayout>

activity文件

package com.example.hello;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
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;

/*
AlertDialog :
show() : 显示警告框
没有公开的构造方法, 只能通过其内部类Builder来创建
AlertDialog.Builder :
create() : 创建AlertDialog对象
show() : 创建AlertDialog对象, 同时将其显示出来
setTitle(CharSequence title) : 设置标题
setMessage(CharSequence message) : 设置内容
setPositiveButton(String text, OnClickListener listener) : 设置正面按钮
setNegativeButton(String text, OnClickListener listener): 设置负面按钮
dismiss() : 移除dialog
setSingleChoiceItems(….)设置单选项列表

自定义AlertDialog需要用到的APi
DialogBuilder :
setView(View view) : 设置Dialog中的视图
View :
View inflate(Context context, int resource, ViewGroup root) : 动态加载布局得到View


*/
public class DialogComponentActivity extends Activity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_component);
}
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(DialogComponentActivity.this, "删除",0).show();;
}
})
.setNegativeButton("取消",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(DialogComponentActivity.this, "取消", 0).show();
}
} )
.show();//这是一条语句,使用的是方法链的调用
}

public void showLD(View v)
{
/*
* 这里的items为什么要设置成final呢?
* 因为在下面这个方法执行完成后,我们还没有点击选择项的时候,
* 这个整个方法已经执行完成,但是我们点击选择项的时候还要使用到
* items里面的值,所以这里设置成final,让它在这个方法执行完成的时候还没消失
*/
final String[] items={"红","绿","蓝","灰"};
new AlertDialog.Builder(this)
.setTitle("选择背景颜色")
.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener()
{
/**
* This method will be invoked when a button in the dialog is clicked.
*
* @param dialog The dialog that received the click.
* @param which The button that was clicked (e.g.
* {@link DialogInterface#BUTTON1}) or the position
* of the item clicked.
* 可以根据下标来确定选择了哪一个选项
*/
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(DialogComponentActivity.this, items[which], 0).show();
dialog.dismiss();
}
})
.show();
}
public void showCD(View v)
{
//动态加载布局文件,得到自定义的View对应的对象
/**
* Inflate a view from an XML resource. This convenience method wraps the {@link
* LayoutInflater} class, which provides a full range of options for view inflation.
*
* @param context The Context object for your activity or application.
* @param resource The resource ID to inflate
* @param root A view group that will be the parent. Used to properly inflate the
* layout_* parameters.
* @see LayoutInflater
* inflate方法是从xml资源加载一个view对象,
* 第三个root参数是指将这个布局加载都这个activity布局的哪里,
* 如果写null就是不加载到那里,意思是不穿插到activity布局中
*/
View view = View.inflate(this, R.layout.dialog, null);
/*
* 虽然我们得到了布局,而且将布局设置进去了,但是我们如何获取
* 布局里面的组件的值呢?
* 之前我们是:
* EditText editText=(EditText) findViewById(R.id.et_dialog_name);
* 但是这样的前提是setContentView(R.layout.activity_dialog_component);
* 将activity_dialog_component这个布局加载到activity才可以找到
* 我们现在如果也是这样找的话肯定是找不到的
*
* 那么上面我们inflate得到的对象就是布局文件的根标签的类型
* 这个对象包含了子标签的类型
* 那么我们就想在上面的view中得到对应的editText,
* 这样调用findViewById才能找到对应的值
*/
final EditText editText1=(EditText) view.findViewById(R.id.et_dialog_name);
final EditText editText2=(EditText) view.findViewById(R.id.et_dialog_pwd);

new AlertDialog.Builder(this)
.setView(view)
.setNegativeButton("取消", null)//不需要监听
.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//获取用户名和密码
String name = editText1.getText().toString();
String pwd = editText2.getText().toString();
Toast.makeText(DialogComponentActivity.this, name+" : "+pwd, 0).show();
}
})
.show();
}

public void showPD(View v)
{
final ProgressDialog show = ProgressDialog.show(this, "加载", "加载中...");
/*
* 长时间的工作不能再主线程执行,这个得启动一个分线程去完成它
* 现在得showPD回调方法就是再主线程执行的
*/
new Thread()
{
public void run()
{
try
{
Thread.sleep(2000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* 开启分线程也有注意点,就是不能在分线程直接更新UI
* 那么下面的show.dismiss();不是直接更新UI了吗?
* 点进去看原码可以发现它是这样执行的
* mHandler.post(mDismissAction);
* dismiss()方法的确是在分线程执行的,但是内部是使用
* Handler实现在主线程移除了show的
* 而下面的
* Toast.makeText(DialogComponentActivity.this, "更新完成", 0).show();
* 就是更新UI了,这样会让这个APP崩掉的
* 如何实现在分线程间接更新UI呢?
* runOnUiThread(new Runnable())
* 在里面重写run()方法,run()方法就会在主线程执行
* 你可能会混淆之前开启一个子线程,也不是重写run()方法吗,
* 然后run()方法不是在子线程执行吗,现在怎么变成在主线程执行了
* 你认真看下,这两个是不一样的
* new Thread(new Runnable(){public void run(){}}).start();
* 这样写Runable的run()方法才是在子线程执行的,
*/
show.dismiss();
//Toast.makeText(DialogComponentActivity.this, "更新完成", 0).show();
runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(DialogComponentActivity.this, "更新完成", 0).show();
}
});

//new Thread(new Runnable(){public void run(){}}).start();
};
}.start();
}
public void showPD2(View v)
{
//1.创建dialog对象
final ProgressDialog progressDialog = new ProgressDialog(this);
//2.设置样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//3.显示
progressDialog.show();
//启动分线程加载数据并且显示进度
new Thread(new Runnable()
{
@Override
public void run()
{
int count=100;
progressDialog.setMax(count);
for(int x=0;x<count;x++)
{
try
{
Thread.sleep(10);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
progressDialog.setProgress(progressDialog.getProgress()+1);
}
progressDialog.dismiss();
}
}).start();
}

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);//得到日

new DatePickerDialog(this, new DatePickerDialog.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); //得到分钟
new TimePickerDialog(this, new OnTimeSetListener()
{

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
Log.e("TAG", hourOfDay+" : "+minute);
}
}, hourOfDay, minute, true).show();
}
}