如何在Android中显示最先进中蜂组合框框

在android应用程序中,经常需要用到dialog对话框让用户知道现在所在进行的操作(比如耗时的操作),或者提示某些信息和状态等,算是比较常用的一个知识点;
1、简单对话框
protected void dialog1(AlertDialog.Builder builder) {
builder.setTitle("标题");
builder.setPositiveButton("确定", null);
builder.setIcon(R.drawable.ic_launcher);
builder.setMessage("简单消息框");
builder.show();
2、带自定义内容对话框
protected void study2(AlertDialog.Builder builder) {
builder.setTitle("标题");
builder.setView(new EditText(this));
builder.setPositiveButton("确定", null);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setMessage("简单消息框");
builder.show();
3、带单选按钮对话框
protected void study3(AlertDialog.Builder builder) {
builder.setTitle("请选择")
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems(
new String[] { "item1", "item2", "item3", "item4" }, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log.e("选择", "" + which);
dialog.dismiss();
}).setNegativeButton("取消", null).show();
在选择了某一项之后,onClick回调事件会把选择的项的索引返回给用户;
4、带多选组合框对话框
protected void study4(AlertDialog.Builder builder) {
builder.setTitle("请选择")
.setIcon(android.R.drawable.ic_dialog_info)
.setMultiChoiceItems(
new String[] { "item1", "item2", "item3", "item4" },
new boolean[] { true, true, false, true },
new OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
ListView lv = ((AlertDialog) dialog).getListView();
Log.e("项" + which, "选择" + lv.getCheckedItemPositions().get(which));
}).setNegativeButton("取消", null).show();
在多选对话框中,选择了某一项并不会导致对话框隐藏,同样android也通过回调接口返回用户所选择的项;
5、进度条对话框
public void study5() {
ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setCancelable(true);
dialog.setMessage("加载中...");
// dialog.setTitle("进度条框窗口");
// dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// dialog.setMax(100);
dialog.show();
6、除了上述的添加用户界面友好提示的方法,还可以通过WindowManager添加一个View到界面上向用户反馈信息,此种方法也更灵活
protected void study6() {
ViewGroup vg = (ViewGroup) getLayoutInflater().inflate(R.layout.dia,null);
pb = (ProgressBar) vg.findViewById(R.id.pb);
vg.removeAllViews();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(160,
160, WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(pb, lp);
重要的步骤即是从lp开始,设置LayoutParams参数,然后添加到窗口;
7.使用Window将view添加到window上
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog dialog = builder.show();
Window window = dialog.getWindow();
window.setContentView(R.layout.dialoglayout);
8.使用style来设置样式
&!-- 自定义Dialog --&
&style name="MyDialog" parent="@android:style/Theme.Dialog"&
&item name="android:windowFrame"&@null&/item&
&item name="android:windowNoTitle"&true&/item&
&item name="android:windowBackground"&@android:color/transparent&/item&
&item name="android:windowIsFloating"&true&/item&
&item name="android:windowContentOverlay"&@null&/item&
Dialog dialog1 = new Dialog(getActivity(), R.style.MyDialog);
dialog1.setContentView(R.layout.dialoglayout);
dialog1.show();
阅读(...) 评论()没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!由 匿名 (未验证)
Android常用控件主要分为4种:
1.文本类控件
TextView是 Android 程序开发中最常用的控件之一,主要功能是向用户展示文本的内容,它是不可编辑的 ,只能通过初始化设置或在程序中修改。
以下介绍一些常见的属性,更多属性可以参考
android:id = "@+id/xxx"
@+id/xxx表示新增控件命名为xxx
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="24sp"
android:textColor="#0000FF"
android:textStyle="normal"
android:gravity="center"
android:singleLine="true"
可编辑文本控件
EditText是可编辑文本控件,可以用来与用户进行交互,其用法和TextView也是类似的,同样,下面介绍一些常见的参数,更多参数可以参考
android:id = "@+id/xxx"
@+id/xxx表示新增控件命名为xxx
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:hint="hello_world"
android:textSize="20sp"
android:textColor="#0000FF"
android:textStyle="normal"
android:gravity="center"
android:singleLine="true"
android:password="true"
android:phoneNumber="true"
android:cursorVisible = "false"
2按钮类控件
Button 按钮
Button控件也是使用过程中用的最多的控件之一,用户可以通过单击 Button 来触发一系列事件,然后为 Button 注册监听器,来实现 Button 的监听事件。
首先来看下Button的配置属性,其实和TextView差不多设置更简单点,主要是显示到Button上面的文字提示:
android:id = "@+id/xxx"
@+id/xxx表示新增控件命名为xxx
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="theButton"
android:textSize="24sp"
android:textColor="#0000FF"
android:textStyle="normal"
android:singleLine="true"
然后我们需要在Activity中为Button的点击事件注册一个监听器,以下介绍两种方式来实现按钮监听事件,更多方法可以参考下Android的按钮单击事件及监听器的实现方式
1.通过匿名内部类作为事件监听器类,这种方法适用于事件监听器只是临时使用一次,因为大部分时候,事件处理器都没有什么利用价值(可利用代码通常都被抽象成了业务逻辑方法),这是一种使用最广泛的方法:
public class MainActivity extends Activity {
private EditT
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext=(EditText) findViewById(R.id.edit_text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
edittext.setText("点击了Button");
2.使用实现接口的方式来进行注册,让Activity类实现了OnClickListener事件监听接口,从而可以在该Activity类中直接定义事件处理器方法:onClick(view v),当为某个组件添加该事件监听器对象时,直接使用this作为事件监听器对象即可:
public class MainActivity extends Activity implements OnClickListener {
private EditT
private Button button2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext=(EditText) findViewById(R.id.edit_text);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(this);
button2.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
edittext.setText("点击了Button");
case R.id.button2:
edittext.setText("点击了Button2");
ImageButton
ImageButton和Button类似,是一个按钮,ImageButton可以实现我们任何想要的图片按钮的效果,比如我们租一个下载的按钮等等。它要比button实现的要好看,并且体验要好很多, 不过它是以图片作为背景,没有文字。利用属性android:src=”图片位置”来设置图片背景。
下面还是先给出一些常见的属性:
&ImageButton
android:id = "@+id/xxx"
@+id/xxx表示新增控件命名为xxx
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src ="@drawable/beautiful"&
RadioButton与RadioGroup
RadioButton(单选按钮)在 Android 平台上也比较常用,比如一些选择项会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在 RadioButton 没有 被选中时,用户通过单击来选中它。但是,在选中后,无法通过单击取消选中。
RadioGroup 是单选组合框,用于 将 RadioButton 框起来。在多个 RadioButton被 RadioGroup 包含的情况下,同一时刻只可以选择一个 RadioButton,并用 setOnCheckedChangeListener 来对 RadioGroup 进行监听。
下面介绍RadioGroup的常用的属性,因为其中包含有RadioButton:
&RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" &
&RadioButton
android:id="@+id/rd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京"
android:textSize="30sp"
android:textColor="#0000FF"
android:textStyle="normal"
&RadioButton
android:id="@+id/rd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="上海" /&
&/RadioGroup&
下面给出在Activity中用 setOnCheckedChangeListener 来对 RadioGroup 进行监听的代码, 注意RadioGroup中的RadioButton也都是需要声明和通过控件的id来得到代表控件的对象。
public class MainActivity extends Activity{
private TextView textV
private RadioG
private RadioButton radiobutton1;
private RadioButton radiobutton2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text_view);
radiogroup = (RadioGroup) findViewById(R.id.radio_group);
radiobutton1 = (RadioButton) findViewById(R.id.rd1);
radiobutton2 = (RadioButton) findViewById(R.id.rd2);
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == radiobutton1.getId()){
textView.setText("北京");
}else if(checkedId == radiobutton2.getId()){
textView.setText("上海");
CheckBox(复选按钮),顾名思义是一种可以进行多选的按钮,默认以矩形表示。与 RadioButton 相同,它也有选中或者不选中双状态。我们可以先在布局文件中定义多选按钮, 然后对每一个多选按钮进行事件监听 setOnCheckedChangeListener,通过 isChecked 来判断 选项是否被选中,做出相应的事件响应。
下面给出CheckBox在布局文件中的常用的属性以及用法:
android:id="@+id/cb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="北京"
android:textSize="30sp"
android:textColor="#0000FF"
android:textStyle="normal"
android:id="@+id/cb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上海"
android:textSize="30sp"
android:textColor="#0000FF"/&
在Activity中调用的代码如下:
public class MainActivity extends Activity{
private TextView textV
private CheckBox checkbox1;
private CheckBox checkbox2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text_view);
checkbox1 = (CheckBox) findViewById(R.id.cb1);
checkbox2 = (CheckBox) findViewById(R.id.cb2);
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
textView.setText("CheckBox选中北京");
checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
textView.setText("CheckBox选中上海");
ImageView 是一个图片控件,负责显示图片,图片的来源可以是系统提供的资源文件,也可以是 Drawable 对象,相对来说,图片空间还是比较好掌握的,因为前面有讲过ImageButton, 很多属性都是相同的。
下面直接给出在布局中的属性:
&ImageView
android:id = "@+id/xxx"
@+id/xxx表示新增控件命名为xxx
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src ="@drawable/beautiful"&
4进度条控件
ProgressBar
ProgressBar 用于在界面上显示一个进度条,表示我们的程序正在加载一些数据,运行程序,会看到屏幕中有一个圆形进度条正在旋转。
在布局xml文件中的用法非常简单:
&ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"/&
android:max="100"
那么如何才能让进度条在数据加载完成时消失呢,这里我们就需要用一开始所讲的Android 控件的可见属性。
可以通过代码来设置控件的可见性,使用的是 setVisibility()方法,可以传入 View.VISIBLE、View.INVISIBLE 和 View.GONE 三种值。
下面实现点击一下按钮让进度条消失,再点击一下按钮让进度条出现的这种效果,这里只给出按钮监听的代码:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (progressBar.getVisibility() == View.GONE) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
至此,关于Android的常用控件都已经讲了一遍,此处再对将关于TextView中文字单独在一行显示的时候实现跑马灯的方法补充下,这里直接给出代码
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode ="true"
android:singleLine="true" /&
添加新评论没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!

我要回帖

更多关于 excel组合框 显示表格 的文章

 

随机推荐