android ProgressBar和ProgressDialog (inderminate)

来源:互联网 发布:mac怎么导出livephoto 编辑:程序博客网 时间:2024/06/05 02:15

一、ProgressBar 原地展示

XML:

<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:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="start" />        <ProgressBar            android:id="@+id/progressBar1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical" >        </ProgressBar>        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="stop" />    </LinearLayout>    <ProgressBar        android:id="@+id/progressBar2"        style="@android:style/Widget.ProgressBar.Small"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical" >    </ProgressBar>    <ProgressBar        android:id="@+id/progressBar3"        style="@android:style/Widget.ProgressBar.Large"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical" >    </ProgressBar>    <ProgressBar        android:id="@+id/progressBar4"        style="@android:style/Widget.ProgressBar.Inverse"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical" >    </ProgressBar>    <ProgressBar        android:id="@+id/progressBar5"        style="@android:style/Widget.ProgressBar.Small.Inverse"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical" >    </ProgressBar>    <ProgressBar        android:id="@+id/progressBar6"        style="@android:style/Widget.ProgressBar.Large.Inverse"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical" >    </ProgressBar>    <ProgressBar        android:id="@+id/progressBar6"        style="@android:style/Widget.ProgressBar.Horizontal"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical" >    </ProgressBar>        </LinearLayout>

Activity:

public class MainActivity extends Activity {private Button btn = null;private Button btn2 = null;private ProgressBar progressBar = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                setupViews();    }        private void setupViews() {    tv = (TextView)findViewById(R.id.textView1);    btn = (Button)findViewById(R.id.button1);    btn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {progressBar.setVisibility(View.VISIBLE);  // 显示}    });    btn2 = (Button)findViewById(R.id.button2);    btn2.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {progressBar.setVisibility(View.GONE);  // 隐藏}    });        progressBar = (ProgressBar)findViewById(R.id.progressBar1);      }}

二、ProgressDialog, 在popup中显示

XML:

<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/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Open ProgressDialog" />        </LinearLayout>



Activity:

public class MainActivity extends Activity {private Button btn = null;private ProgressDialog pd;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                setupViews();    }        private void setupViews() {    btn = (Button)findViewById(R.id.button);    btn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {pd = ProgressDialog.show(MainActivity.this, null, "Loading...", true, true);new Thread() {public void run(){try {Thread.sleep(2000);  // 业务处理过程} catch (Exception e) {e.printStackTrace();}handler.sendEmptyMessage(0);  // 更新UI}}.start();}    });    }    private Handler handler = new Handler(){          public void handleMessage(Message msg) {          pd.dismiss();          }};}






0 0
原创粉丝点击