android 各种常用,不易记的dialog,等控件整理
16lz
2021-12-04
android 各种常用,不易记的dialog,等控件整理
热3 已有 267 次阅读 2012-08-27 09:26 标签: android 的 view source print ?001 | package com.su.testalldialog; |
002 |
|
003 | import java.util.ArrayList; |
004 |
|
005 | import android.app.Activity; |
006 | import android.app.AlertDialog; |
007 | import android.app.AlertDialog.Builder; |
008 | import android.content.Context; |
009 | import android.content.DialogInterface; |
010 | import android.os.Bundle; |
011 | import android.view.View; |
012 | import android.view.View.OnClickListener; |
013 | import android.widget.AdapterView; |
014 | import android.widget.AdapterView.OnItemSelectedListener; |
015 | import android.widget.ArrayAdapter; |
016 | import android.widget.AutoCompleteTextView; |
017 | import android.widget.Button; |
018 | import android.widget.EditText; |
019 | import android.widget.ImageView; |
020 | import android.widget.Spinner; |
021 | import android.widget.Toast; |
022 |
|
023 | public class TestAllDialogActivity extends Activity { |
024 |
String[] weekdays; |
025 |
Context context; |
026 |
|
027 |
public void onCreate(Bundle savedInstanceState) { |
028 |
super .onCreate(savedInstanceState); |
029 |
setContentView(R.layout.main); |
030 |
context = this ; |
031 |
weekdays = new String[] { "Sun" , "Mon" , "Tue" , "Wed" }; |
032 |
Button button1 = (Button) findViewById(R.id.button1); |
033 |
Button button2 = (Button) findViewById(R.id.button2); |
034 |
Button button3 = (Button) findViewById(R.id.button3); |
035 |
Button button4 = (Button) findViewById(R.id.button4); |
036 |
Button button5 = (Button) findViewById(R.id.button5); |
037 |
Button button6 = (Button) findViewById(R.id.button6); |
038 |
button6.setOnClickListener(listener); |
039 |
button5.setOnClickListener(listener); |
040 |
button1.setOnClickListener(listener); |
041 |
button2.setOnClickListener(listener); |
042 |
button3.setOnClickListener(listener); |
043 |
button4.setOnClickListener(listener); |
044 |
initSpinner(); |
045 |
initAutocompletetextview(); |
046 |
|
047 |
} |
048 |
|
049 |
private void initAutocompletetextview() { |
050 |
AutoCompleteTextView autocompletetextview = (AutoCompleteTextView) findViewById(R.id.autocompletetextview); |
051 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, |
052 |
android.R.layout.simple_spinner_dropdown_item, weekdays); // 实现简单的adapter |
053 |
autocompletetextview.setAdapter(adapter); |
054 |
} |
055 |
|
056 |
private void initSpinner() { |
057 |
Spinner spinner = (Spinner) findViewById(R.id.spinner); |
058 |
spinner.setOnItemSelectedListener(slistener); |
059 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, |
060 |
android.R.layout.simple_spinner_item, weekdays); // 实现简单的adapter |
061 |
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 实现选项的样式 |
062 |
spinner.setAdapter(adapter); |
063 |
} |
064 |
|
065 |
private OnItemSelectedListener slistener = new OnItemSelectedListener() { |
066 |
|
067 |
@Override |
068 |
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, |
069 |
long arg3) { |
070 |
toast(weekdays[arg2]); |
071 |
|
072 |
} |
073 |
|
074 |
@Override |
075 |
public void onNothingSelected(AdapterView<?> arg0) { |
076 |
// TODO Auto-generated method stub |
077 |
|
078 |
} |
079 |
}; |
080 |
|
081 |
int index = 0 ; // 用来标记哪一项被选择 |
082 |
private OnClickListener listener = new OnClickListener() { |
083 |
|
084 |
@Override |
085 |
public void onClick(View v) { |
086 |
|
087 |
Builder dialog = new AlertDialog.Builder(context); |
088 |
switch (v.getId()) { |
089 |
|
090 |
case R.id.button1: |
091 |
dialog.setIcon(R.drawable.ic_launcher); |
092 |
dialog.setTitle( "简单选择对话框" ); |
093 |
dialog.setItems(weekdays, |
094 |
new DialogInterface.OnClickListener() { |
095 |
|
096 |
@Override |
097 |
public void onClick(DialogInterface dialog, |
098 |
int which) { |
099 |
toast(weekdays[which]); |
100 |
|
101 |
} |
102 |
}); |
103 |
dialog.show(); |
104 |
break ; |
105 |
case R.id.button2: |
106 |
dialog.setIcon(R.drawable.ic_launcher); |
107 |
dialog.setTitle( "单选对话框" ); |
108 |
dialog.setSingleChoiceItems(weekdays, 1 , |
109 |
new DialogInterface.OnClickListener() { |
110 |
|
111 |
public void onClick(DialogInterface dialog, |
112 |
int which) { |
113 |
index = which; |
114 |
|
115 |
} |
116 |
}); |
117 |
dialog.setPositiveButton( "是" , |
118 |
new DialogInterface.OnClickListener() { |
119 |
public void onClick(DialogInterface dialog, int i) { |
120 |
toast(weekdays[index]); |
121 |
} |
122 |
}); |
123 |
dialog.setNegativeButton( "否" , |
124 |
new DialogInterface.OnClickListener() { |
125 |
public void onClick(DialogInterface dialog, int i) { |
126 |
|
127 |
} |
128 |
}); |
129 |
|
130 |
dialog.show(); |
131 |
break ; |
132 |
case R.id.button3: |
133 |
final ArrayList<String> multiWeekadys = new ArrayList<String>(); // 选择的数据放到list中 |
134 |
|
135 |
dialog.setIcon(R.drawable.ic_launcher); |
136 |
dialog.setTitle( "复选对话框" ); |
137 |
final boolean [] checkeditems = new boolean [weekdays.length]; // 设置一打开时的选择情况 |
138 |
dialog.setMultiChoiceItems(weekdays, checkeditems, |
139 |
new DialogInterface.OnMultiChoiceClickListener() { |
140 |
|
141 |
@Override |
142 |
public void onClick(DialogInterface dialog, |
143 |
int which, boolean isChecked) { |
144 |
if (isChecked) { |
145 |
checkeditems[which] = true ; // 获取一个被选中的列表 |
146 |
|
147 |
} |
148 |
|
149 |
} |
150 |
}); |
151 |
|
152 |
dialog.setPositiveButton( "是" , |
153 |
new DialogInterface.OnClickListener() { |
154 |
public void onClick(DialogInterface dialog, int i) { |
155 |
for ( int j = 0 ; j < checkeditems.length; j++) { // 遍历一遍看那个是选中的 |
156 |
if (checkeditems[j]) { |
157 |
multiWeekadys.add(weekdays[j]); |
158 |
} |
159 |
|
160 |
} |
161 |
|
162 |
toast(multiWeekadys.toString()); |
163 |
} |
164 |
}); |
165 |
dialog.setNegativeButton( "否" , |
166 |
new DialogInterface.OnClickListener() { |
167 |
public void onClick(DialogInterface dialog, int i) { |
168 |
|
169 |
} |
170 |
}); |
171 |
dialog.show(); |
172 |
break ; |
173 |
case R.id.button4: |
174 |
dialog.setIcon(R.drawable.ic_launcher); |
175 |
dialog.setTitle( "简单警告" ); |
176 |
dialog.setMessage( "测试警告" ); |
177 |
dialog.setPositiveButton( "是" , |
178 |
new DialogInterface.OnClickListener() { |
179 |
public void onClick(DialogInterface dialog, int i) { |
180 |
toast( "是" ); |
181 |
|
182 |
} |
183 |
}); |
184 |
dialog.setNeutralButton( "中" , |
185 |
new DialogInterface.OnClickListener() { |
186 |
public void onClick(DialogInterface dialog, int i) { |
187 |
toast( "中" ); |
188 |
} |
189 |
}); |
190 |
dialog.setNegativeButton( "否" , |
191 |
new DialogInterface.OnClickListener() { |
192 |
public void onClick(DialogInterface dialog, int i) { |
193 |
|
194 |
} |
195 |
}); |
196 |
|
197 |
dialog.show(); |
198 |
break ; |
199 |
case R.id.button5: |
200 |
final EditText et = new EditText(context); |
201 |
et.setText( "测试" ); |
202 |
dialog.setIcon(R.drawable.ic_launcher); |
203 |
dialog.setTitle( "简单警告" ); |
204 |
dialog.setMessage( "测试警告" ); |
205 |
dialog.setView(et); |
206 |
dialog.setPositiveButton( "是" , |
207 |
new DialogInterface.OnClickListener() { |
208 |
public void onClick(DialogInterface dialog, int i) { |
209 |
toast(et.getText().toString()); |
210 |
|
211 |
} |
212 |
}); |
213 |
dialog.setNeutralButton( "中" , |
214 |
new DialogInterface.OnClickListener() { |
215 |
public void onClick(DialogInterface dialog, int i) { |
216 |
toast( "中" ); |
217 |
} |
218 |
}); |
219 |
dialog.setNegativeButton( "否" , |
220 |
new DialogInterface.OnClickListener() { |
221 |
public void onClick(DialogInterface dialog, int i) { |
222 |
|
223 |
} |
224 |
}); |
225 |
|
226 |
dialog.show(); |
227 |
break ; |
228 |
case R.id.button6: |
229 |
|
230 |
final ImageView im = new ImageView(context); |
231 |
im.setImageResource(R.drawable.ic_launcher); |
232 |
|
233 |
dialog.setIcon(R.drawable.ic_launcher); |
234 |
dialog.setTitle( "简单警告" ); |
235 |
dialog.setMessage( "测试警告" ); |
236 |
dialog.setView(im); |
237 |
|
238 |
dialog.setPositiveButton( "是" , |
239 |
new DialogInterface.OnClickListener() { |
240 |
public void onClick(DialogInterface dialog, int i) { |
241 |
|
242 |
} |
243 |
}); |
244 |
dialog.setNeutralButton( "中" , |
245 |
new DialogInterface.OnClickListener() { |
246 |
public void onClick(DialogInterface dialog, int i) { |
247 |
toast( "中" ); |
248 |
} |
249 |
}); |
250 |
dialog.setNegativeButton( "否" , |
251 |
new DialogInterface.OnClickListener() { |
252 |
public void onClick(DialogInterface dialog, int i) { |
253 |
|
254 |
} |
255 |
}); |
256 |
|
257 |
dialog.show(); |
258 |
break ; |
259 |
|
260 |
default : |
261 |
break ; |
262 |
} |
263 |
|
264 |
} |
265 |
}; |
266 |
|
267 |
private void toast(String msg) { |
268 |
Toast.makeText(context, msg, 10 ).show(); |
269 |
|
270 |
} |
271 | } |
更多相关文章
- Android在屏幕任意位置显示对话框
- Android(安卓): Android(安卓)Studio 3.4更新至gradle 5.1.1的问
- Android(安卓)远程监控摄像头 移动端+PC端 旧手札变废为宝
- Android开发UI之自定义控件的皮肤
- Android(安卓)七种对话框使用
- Android开发环境配置
- Activity has leaked window that was originally added 问题解
- android简单的日期时间选择器
- Android之Android(安卓)Studio常用插件