android控件

2019-04-15 16:53发布

data/attach/1904/ne42fnmfk3x70gevj2w4nv11mv8cpm7m.jpg Button , MediaPlayer, SoundPool,ListView,Spinner,checkBox,RadioGroup,TimePicker
DatePicker,多行EditText

alert提示框

[img]http://dl.iteye.com/upload/picture/pic/103030/a87c48e1-430c-33f8-9b05-171911124d8a.png[/img]
[code]
private Dialog buildDialog1(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);//三角形中间一个感叹号的图
builder.setTitle(R.string.alert_dialog_two_buttons_title);//提示的文字
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

setTitle("点击了对话框上的确定按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
[/code]

[img]http://dl.iteye.com/upload/picture/pic/103032/a35e94ca-7346-3e61-abd5-2a3776dcfa53.png[/img]
[code]
private Dialog buildDialog2(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);//同上
builder.setTitle(R.string.alert_dialog_two_buttons_msg);
builder.setMessage(R.string.alert_dialog_two_buttons2_msg);// 中间一大段文字
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

setTitle("点击了对话框上的确定按钮");
}
});
builder.setNeutralButton(R.string.alert_dialog_something,
new DialogInterface.OnClickListener() {//中间的按钮
public void onClick(DialogInterface dialog, int whichButton) {

setTitle("点击了对话框上的进入详细按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
[/code]

[img]http://dl.iteye.com/upload/picture/pic/103034/b889cb6f-3c63-3e0d-bc41-bb47c1132c37.png[/img]
[code]
private Dialog buildDialog3(Context context) {
LayoutInflater inflater = LayoutInflater.from(this);//这个类专门用来实例化一个xml
final View textEntryView = inflater.inflate(
R.layout.alert_dialog_text_entry, null);//加载一个xml
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_text_entry);
builder.setView(textEntryView);//设置xml
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
[/code]
[img]http://dl.iteye.com/upload/picture/pic/103036/43256593-80b1-3c56-884b-930d4a5be5ca.png[/img]
[code]
private Dialog buildDialog4(Context context) {
ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle("正在下载歌曲");
dialog.setMessage("请稍候……");
return dialog;
}
[/code]
-----------------------------------------------------------
button
[img]http://dl.iteye.com/upload/picture/pic/102528/339e332f-8c98-31ae-a84b-96830fb5d22c.jpg[/img]
[img]http://dl.iteye.com/upload/picture/pic/102530/60e79f71-7429-3ef2-a001-3bf2fe46d025.jpg[/img]

[code]
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按钮"
>

android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img"
>

android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

[/code]

监听事件
[code]
public void onClick(View v) {//重写的事件处理回调方法
if(v == button){//点击的是普通按钮
textView.setText("您点击的是普通按钮");
}
else if(v == imageButton){//点击的是图片按钮
textView.setText("您点击的是图片按钮");
}
else if(v == toggleButton){//点击的是开关按钮
textView.setText("您点击的是开关按钮");
}
}
[/code]

------------------------------------------------------
播放声音控件
[code]
public void initSounds(){//初始化声音的方法
mMediaPlayer = MediaPlayer.create(this, R.raw.backsound);//初始化MediaPlayer
//soundPool第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质;
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap(); //存入多个音频的ID,播放的时候可以同时播放多个音频
soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong, 1));//API中指出,其中的priority参(第三个)目前没有效果,建议设置为1
}
public void playSound(int sound, int loop) {//用SoundPoll播放声音的方法
AudioManager mgr = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent/streamVolumeMax;
//play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
//loop —— 循环播放的次数,0为值播放一次,-1为无限循环
//rate —— 播放的速率,范围0.5-2.0(0.5为一半速率,1.0为正常速率,2.0为两倍速率)
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);//播放声音
}
public void onClick(View v) {//实现接口中的方法
// TODO Auto-generated method stub
if(v == button1){//点击了使用MediaPlayer播放声音按钮
textView.setText("使用MediaPlayer播放声音");
if(!mMediaPlayer.isPlaying()){
mMediaPlayer.start();//播放声音
}
}
else if(v == button2){//点击了暂停MediaPlayer声音按钮
textView.setText("暂停了MediaPlayer播放的声音");
if(mMediaPlayer.isPlaying()){
mMediaPlayer.pause();//暂停声音
}
}
else if(v == button3){//点击了使用SoundPool播放声音按钮
textView.setText("使用SoundPool播放声音");
this.playSound(1, 0);
}
else if(v == button4){//点击了暂停SoundPool声音按钮,
textView.setText("暂停了SoundPool播放的声音");
soundPool.pause(1);//暂停SoundPool的声音
}
}
[/code]

--------------------------------------------------------------------
ListView

item样式文件
[code]
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="姓名"
android:id="@+id/name"
/>

android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="电话"
android:id="@+id/phone"
/>

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="存款"
android:id="@+id/amount"
/>

[/code]
main.xml中的ListView
[code]
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView"
/>
[/code]

[code]
listView = (ListView)this.findViewById(R.id.listView);
List persons = service.getScrollData(0, 5); //得到数据
List> data = new ArrayList>();
for(Person person : persons){//装入List
HashMap item = new HashMap();
item.put("name", person.getName());
item.put("phone", person.getPhone());
item.put("amount", person.getAmount());
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
new String[]{"name","phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});//数据,key,样式绑定
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ItemClickListener());

或者:
Cursor cursor = service.getCursorScrollData(0, 5);
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor,
new String[]{"name","phone","amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
listView.setAdapter(cursorAdapter);

[/code]

-------------------------------------------------------------

下拉框
[img]http://dl.iteye.com/upload/picture/pic/102672/dfa667db-4e98-3e29-9244-8d871e708c5c.jpg[/img]
[img]http://dl.iteye.com/upload/picture/pic/102674/1f3fcf21-455f-3a6d-acb5-29060eede95f.jpg[/img]

方法一: 纯xml
xml控件,且数据关联写死在xml中
[code]
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/app_name"
android:entries="@array/city_arr"
/>
[/code]
xml数据
[code]

长沙
南京
杭州
湘潭

[/code]

方法二:
[code]
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/app_name"
--entries没了,在程序里面绑定
/>
[/code]

[code]
spinner = (Spinner)findViewById(R.id.myspinner);
spinner.setPrompt("选择城市");

this.adapterColor = ArrayAdapter.createFromResource(this, R.array.city_arr, android.R.layout.simple_spinner_item);
//第三个参数是样式
adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //重置样式,也就是第三个参数

[/code]

方法三:数据也不需要去取了

[code]
List list = new ArrayList();
list.add("长沙");
list.add("南京");
list.add("北京");
this.adapterColor = new ArrayAdapter(this, android.R.layout.simple_spinner_item,list);
this.adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(this.adapterColor);

[/code]
事件:
[code]
citySpinner = (Spinner)findViewById(R.id.sp);
citySpinner.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
private class OnItemSelectedListenerImpl implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView parent, View view, int position,
long id) {//表示选项改变时候触发
String item = parent.getItemAtPosition(position).toString(); //得到选项的值
//...
}
@Override
public void onNothingSelected(AdapterView parent) {//表示没有选则时候触发
}
}
[/code]
二级联动菜单
[code]
private Spinner citySpinner;
private Spinner areaSpinner;
String[][] areaData = new String[][]{
{"aa","bb","cc","cc"},{"aa2","bb2","cc2"},{"aa3","bb3","cc3"}
};

citySpinner = (Spinner)findViewById(R.id.sp);
citySpinner.setOnItemSelectedListener(new OnItemSelectedListenerImpl());


private class OnItemSelectedListenerImpl implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView parent, View view, int position,
long id) {//表示选项改变时候触发
TestAndroidActivity.this.adpterArea = new ArrayAdapter(TestAndroidActivity.this,
android.R.layout.simple_spinner_item,
TestAndroidActivity.this.areaData[position]);//把二级数据放入适配器
TestAndroidActivity.this.areaSpinner.setAdapter(TestAndroidActivity.this.adpterArea);
}
@Override
public void onNothingSelected(AdapterView parent) {//表示没有选则时候触发
}
}
[/code]

----------------------------------------------------------------------
复选框---就是一个按钮而已,和单选框差别很大
[img]http://dl.iteye.com/upload/picture/pic/102758/f85a4bdc-636c-31b8-832a-aad7d8348070.jpg[/img]
[code]
android:id="@+id/url1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="www.aa.com"
/>
android:id="@+id/url2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="www.bb.com" ---文字在xml中
/>
android:id="@+id/url3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
[/code]
[code]
CheckBox url3 = (CheckBox)findViewById(R.id.url3);
url3.setChecked(true);
url3.setText("www.zzz.com");
[/code]

-----------------------------------------------------------
单选钮
[img]http://dl.iteye.com/upload/picture/pic/102760/d29f3a3f-b81f-3663-b9f1-da088ae5b69a.jpg[/img]
xml
[code]
android:id="@+id/encoding"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" --控制方向
android:checkedButton="@+id/gbk_btn"> ---默认选中,这里要有加号

android:id="@+id/gbk_btn" ---这里也要有加号
android:text="gbk编码"/>
android:id="@+id/utf8_btn"
android:text="utf8编码"/>

[/code]

[code]
sex = (RadioGroup)findViewById(R.id.sex);
male = (RadioButton)findViewById(R.id.male);
female = (RadioButton)findViewById(R.id.female);
this.sex.setOnCheckedChangeListener(new setOnCheckedChangeListenerImpl());

private class setOnCheckedChangeListenerImpl implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(TestAndroidActivity.this.male.getId()==checkedId){
}else if(TestAndroidActivity.this.female.getId()==checkedId){
}
}
}
[/code]

---------------------------------------------------
图片显示控件, 就是显示一张图片 ,和TextView差不多,一个显示文字,一个显示图片
[code]
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
[/code]

---------------------------------------------------
TimePicker 时间管理器 ---时,分
[img]http://dl.iteye.com/upload/picture/pic/102766/04ba456b-fb67-314e-83b0-d438354ccb0a.png[/img]
xml
[code]
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tp01"
/>
[/code]
[code]
TimePicker tp = (TimePicker)findViewById(R.id.tp01);
tp.setIs24HourView(true);
tp.setCurrentHour(15);
tp.setCurrentMinute(33);
tp.setOnTimeChangedListener(new OnTimeChangedListenerImpl());
private class OnTimeChangedListenerImpl implements OnTimeChangedListener {

@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
//当按加减键的时候触发此方法


}
}
[/code]

--------------------------------------------
日期管理器-----年月日
[img]http://dl.iteye.com/upload/picture/pic/102768/f7bf19ea-e2c9-322d-b20f-6758704af3a1.png[/img]
[code]
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dp01"
/>
[/code]
[code]
DatePicker dp = (DatePicker)findViewById(R.id.dp01);
dp.updateDate(1998, 7, 21);
dp.init(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(),new OnDateChangedListenerImpl());
private class OnDateChangedListenerImpl implements OnDateChangedListener{
//按加减按钮的时候触发此方法
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
}

}
[/code]

-----------------
EditText多行
[code]
android:layout_height="120.0dip"
android:inputType="textMultiLine"
android:singleLine="false"
android:gravity="left|top"/>
[/code]