您的位置:首页 > 产品设计 > UI/UE

【Android 开发】:UI控件之 Dialogs 对话框控件的的使用(三)

2013-08-09 19:05 633 查看
    前面两讲我们讲解了AlertDialog的一些基本使用方法,下面我在来学习对话框的其他使用方法,ProgressDialog 和自定义对话框。

1. ProgressDialog进度条的使用。

    ProgressDialog是AlertDialog的一个子类,主要是显示一个旋转动画形式的进度对话框,或者是一个进度条,它一般表示任务在执行过程中的进度显示,这种对话框还可提供一个按钮,比如说用于取消下载的按钮。通过调用ProgressDialog.show()来显示这种对话框。
    查看ProgressDialog的API文档,进度对话框可以通过back键取消,进度范围在 0~10000之间,一般是在加载网络数据的时候使用。以后在做百分比的时候一般在这边要做一个下载量和百分比的一个公式的换算。查看方法 public void setProgressStyle
(int style),这个方法用来显示对话框的样式。查看该API下两个常量,如下图所示:



    第一种是用来做用来下载的进度,后面一种主要是延时的操作。

使用方式如下说明:

To show the progression with an animated progress bar:
    1. Initialize the ProgressDialog with the class constructor, ProgressDialog(Context).
    2. Set the progress style to "STYLE_HORIZONTAL" with setProgressStyle(int) and set any other properties, such as the message.
    3. When you're ready to show the dialog, call show() or return the ProgressDialog from the onCreateDialog(int) callback.
    4. You can increment the amount of progress displayed in the bar by calling either setProgress(int) with a value for the total percentage completed so far or incrementProgressBy(int) with an incremental
value to add to the total percentage completed so far.

2. 自定义对话框

    如果你想要自己定义对话框,你可以给你的窗体创建自己的布局,创建好布局之后,根据布局对象和布局资源ID在setContentView(View)中设置即可。

3. 代码的实现

1) 布局文件 activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="显示进度对话框一" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="显示进度对话框二" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="自定义对话框" />

</RelativeLayout> 
2) 自定义布局 custom_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" >

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF" />

</LinearLayout>
3) 自定义布局对话框代码 CustomDialog.java

package com.android.progressdialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/*
* 注意在这边最好不要用 CustomDialog 去继承 Dialog ,这样会造成栈溢出的异常
* 最好用一下代码方式去实现,在初始化中返回一个Dialog类的实例即可。
*/
public class CustomDialog {

private Context context;
private Dialog dialog;

public CustomDialog(Context context) {
this.context = context;
dialog = new Dialog(context);
}

public void show(){
View view = LayoutInflater.from(context).inflate(R.layout.custom_dialog, null);
// dialog.setContentView(R.layout.custom_dialog); //也可以使用这一种方式来加载自定义布局
dialog.setContentView(view);
dialog.setTitle("自定义对话框");
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText("这是一个自定义对话框!");
ImageView imageView = (ImageView) view.findViewById(R.id.image);
imageView.setImageResource(R.drawable.ic_launcher);
dialog.show();
}

}

4) 程序主要代码 MainActivity.java

package com.android.progressdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private Button button1;
private Button button2;
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponent();
button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//ProgressDialog.show(MainActivity.this, "提示", "正在加载,请稍后......"); 第一种写法
ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("提示");
dialog.setMessage("正在加载,请稍后.....");
dialog.show(); //显示对话框
//dialog.dismiss(); //隐藏对话框,注意的是这个对话框的显示和隐藏不能在同一时刻用,需要结合线程或者消息来使用。

}
});
button2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//第一种显示方式
//ProgressDialog dialog = new ProgressDialog(MainActivity.this, ProgressDialog.STYLE_HORIZONTAL);
ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("下载提示");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(50); //注意到这个设置为固定的值50,运行是没有变化的,这边需要在加上线程的操作才可以进行变化。
dialog.setCancelable(false); //如果设置为false,则用户在点击对话框其他焦点的时候,是无效,这样设计是为了防止系统在处理过程中出现其他情况。
dialog.show();
}
});
button3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CustomDialog dialog = new CustomDialog(MainActivity.this);
dialog.show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private void initComponent(){
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
}

}


4. 程序运行结果


        


       


      

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐