当前位置: 代码迷 >> Android >> android基础入门SeekBar跟ProgressDialog(9)
  详细解决方案

android基础入门SeekBar跟ProgressDialog(9)

热度:93   发布时间:2016-04-28 06:30:22.0
android基础入门SeekBar和ProgressDialog(9)

一.SeekBar(拖动条):

       拖动条类似于进度条,两者不相同的是用户可以控制,例如,应用程序中用户可以对音效进行控制,这就可以使用拖动条来实现,由于拖动条可以被用户控制,所以需要对其进行事件监听,这就需要实现SeekBar.OnSeekBarChangeListener接口,在SeekBar中共需要监听3个事件,分别是:onStopTrackingTouch(停止拖动)、onStartTrackingTouch (开始拖动)、onProgressChanged(数值的改变)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <SeekBar        android:id="@+id/seekBar1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:max="100"        android:progress="50"        android:secondaryProgress="40"         />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView" /></LinearLayout>


实现代码:

public class MainActivity extends Activity {	SeekBar mSeekBar;	TextView t1;	TextView t2;		@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);				mSeekBar = (SeekBar)findViewById(R.id.seekBar1);				t1 = (TextView)findViewById(R.id.textView1);		t2 = (TextView)findViewById(R.id.textView2);        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {						@Override			public void onStopTrackingTouch(SeekBar seekBar) {				// TODO Auto-generated method stub				t2.setText("停止调节");			}						@Override			public void onStartTrackingTouch(SeekBar seekBar) {				// TODO Auto-generated method stub				t2.setText("正在调节");			}						@Override			public void onProgressChanged(SeekBar seekBar, int progress,					boolean fromUser) {				// TODO Auto-generated method stub				t1.setText("当前值:"+progress);			}		});												}	@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;	}}

运行图片:

当我们点击拖动条时:

当我们松开时:


二.ProgressDialog(对话框中的进度条) :

    PrigressDialog是对话框和进度条的结合体。

 实例:

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="两种对话框进度条使用效果" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="圆形进度条" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="长形进度条" />    </LinearLayout>


主要代码:

public class MainActivity extends Activity {	private Button b1 , b2;	ProgressDialog pd ;		int m_count = 0;		@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		b1 = (Button)findViewById(R.id.button1);		b2 = (Button)findViewById(R.id.button2);						b1.setOnClickListener(new View.OnClickListener() {						@SuppressWarnings("deprecation")			@Override			public void onClick(View v) {               				pd = new ProgressDialog(MainActivity.this);				pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);				pd.setTitle("提示");                pd.setMessage("圆形进度条对话框");                pd.setIcon(R.drawable.ic_launcher);                pd.setIndeterminate(false);                pd.setCancelable(true);                pd.setButton("确定", new DialogInterface.OnClickListener() {										@Override					public void onClick(DialogInterface dialog, int which) {												dialog.cancel();											}				});                pd.show();			}		});       b2.setOnClickListener(new View.OnClickListener() {						@SuppressWarnings("deprecation")			@Override			public void onClick(View v) {               				m_count = 0;				pd = new ProgressDialog(MainActivity.this);				pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);				pd.setTitle("提示");                pd.setMessage("长条进度条对话框");                pd.setIcon(R.drawable.ic_launcher);                pd.setIndeterminate(false);                pd.setCancelable(true);                pd.setButton("确定", new DialogInterface.OnClickListener() {										@Override					public void onClick(DialogInterface dialog, int which) {												dialog.cancel();											}				});                pd.show();                new Thread(){                	public void run(){                		                		                		try {                			while(m_count <= 100){                			pd.setProgress(m_count++);                										Thread.sleep(100);                			}						} catch (InterruptedException e) {							// TODO Auto-generated catch block							e.printStackTrace();						}                		                		                	}                	                }.start();			}		});	}	@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;	}}


ProgressDialog中调用的方法:

1.setProgressStyle:设置进度条风格,风格为圆形,选装的。

2.setTitle:设置ProgressDialog标题。

3.setMessage:设置ProgressDialog提示信息。

4.setIcon:设置ProgressDialog标题图标。

5.setIndeterminate:设置ProgressDialog的进度条是否明确。

6.setCancelable:设置ProgressDialog是否可以按退回按键取消。

7.setButton:设置ProgressDialog的一个Button和监听Button事件。

8.show:显示ProgressDialog。


运行实例:




  相关解决方案