首页 > 代码库 > 关于Activity的少许细节

关于Activity的少许细节

1.  对活动应用样式和主题

2.  隐藏活动标题

3. 显示对话框窗口

4. 显示进度对话框

 

1.  应用样式和主题

image

改成

android:theme="@android:style/Theme.Dialog"

效果如下:

image


2. 隐藏活动的标题

比方说手机QQ登陆的时候,就会有一个全屏的QQ画面;说着微博也是,有一个全屏广告

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏标题                setContentView(R.layout.activity_main);        Log.i(Tag, "onCreat()");    }

标题没有了:(我应该在这里放一幅画)

image


3.显示对话框窗口

(主要思想:重写Activity类中的onCreateDialog()方法)

(这里还谈到了“回调方法”目前先放一放)

 

现在布局文件里面加入一个button,然后在activity中绑定事件,然后在onClick()中填入代码

<Button      android:id="@+id/btn1"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:text="显示对话框" />
MainActivity.java中的代码是:(事件监听我知道,回调不太清楚,但是showDialog()方法被调用的时候,onCreateDialog()方法被调用了)
public class MainActivity extends Activity implements OnClickListener{    private Button btn;    CharSequence[] items = {"你","我","他"};    boolean[] checkedItems = new boolean[items.length];        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                btn = (Button) findViewById(R.id.btn1);        btn.setOnClickListener(this);            }    @Override    public void onClick(View v)     {        showDialog(0);  //坑爹啊,这方法过时了?    }            @Override    @Deprecated    protected Dialog onCreateDialog(int id)     {        //下面填代码        switch (id)         {            case 0:                return new AlertDialog.Builder(this)                                .setIcon(R.drawable.ic_launcher)                                .setTitle("这是个简单的对话框..")                                .setPositiveButton("OK",                                        new DialogInterface.OnClickListener()                                         {                                                                                        @Override                                            public void onClick(DialogInterface dialog, int which)                                             {                                                Toast.makeText(getBaseContext(),                                                         "OK被点击了", Toast.LENGTH_LONG).show();                                                //Toast是一种提示框,Toast.LENGTH_LONG表示它显示时间长                                            }                                        })                                .setNegativeButton("Cancel",new DialogInterface.OnClickListener()                                 {                                                                        @Override                                    public void onClick(DialogInterface dialog, int which)                                     {                                        Toast.makeText(getBaseContext(),                                                 "Cancel被点击了", Toast.LENGTH_LONG).show();                                        //Toast是一种提示框,Toast.LENGTH_LONG表示它显示时间长                                    }                                })                                .setMultiChoiceItems(items,                                                     checkedItems,                                                     new DialogInterface.OnMultiChoiceClickListener()                                                     {                                                                                                                @Override                                                        public void onClick(DialogInterface dialog,                                                                 int which,                                                                 boolean isChecked)                                                         {                                                            Toast.makeText(getBaseContext(),                                                                     items[which]+(isChecked?"Checked!":"unCkecked!"),                                                                     Toast.LENGTH_LONG).show();                                                        }                                                     })                                .create();            case 1: break;        default:                break;        }        return null;    }}

image

image

image

image

image

大概玩明白咋回事儿了。

showDialog()一旦被执行,它就去调用onCreateDialog()这个由Activity管理的方法。------回调

 

补充:

11 Toast android.widget.Toast.makeText(Context context, CharSequence text, int duration)

Make a standard toast that just contains a text view.

Parameters:
context The context to use. Usually your android.app.Application or android.app.Activity object.
text The text to show. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

 

实际上,再看上面的代码,

setPositiveButton(String text,new DialogInterface.OnClickListener(){})  这方法设置了一个OK按钮

setNegativeButton(String text,new DialogInterface.OnClickListener(){})  这方法设置了一个Cancel按钮

setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,new DialogInterface.OnMultiChoiceClickListener()) 设置了一个复选框列表

 

而且代码一开始的

      new AlertDialog.Builder(this)

   实际上返回 Builder对象,完全可以写成:

              Builder builder = new AlertDialog.Builder(this);

然后就是跟上面代码差不多:

              builder.setPositiveButton();

              builder.setNegativeButton();

                    ….

switch代码块里面返回 builder.create()就好了

 

程序中多次用到Context类以及其引用,实际上,Activity是Context的子类

如果在activity中引用context对象,那么可以在外部成员变量的地方声明一个 :

private Context context = this;

之后就不用总是getBaseContext(); 而直接用context就好了。

但是上面的Toast.makeText(getBaseContext(),”ok clicked”,Toast.LENGTH_LONG)由于toast在AlertDialog中使用,所以还是应该用getBaseContext()


4. 显示进度对话框

基本上就是尝试着用代码开一个线程去做后台的事儿---假装我们在做什么,前台呢?显示模态进度条对话框,由于是模态(操作系统术语)所以阻塞了用户界面。

public class MainActivity extends Activity implements OnClickListener{    private Button btn;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                btn = (Button) findViewById(R.id.btn1);        btn.setOnClickListener(this);            }    @Override    public void onClick(View v)     {        final ProgressDialog dialog = ProgressDialog.show(this,"我们在处理中", "拼命加载中",true);                new Thread(new Runnable()        {            @Override            public void run()             {//                try //                {//                    Thread.sleep(5000); //假装我们在做什么//                    //好,我们已经做完了,等够5秒了//                    dialog.dismiss();//                } //                catch (Exception e) //                {//                    e.printStackTrace();//                }                SystemClock.sleep(5000);//假装我们在做什么,这样写不用try catch                dialog.dismiss();            }                    }).start();                }}
image

image

然后5秒,这进度条就不见了.

dialog.dismiss()了

 

既然要假装做点儿什么,来做点儿实际的:

修改MainActivity .java

public class MainActivity extends Activity implements OnClickListener{    private Button btn;    private ProgressDialog progressDialog;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                btn = (Button) findViewById(R.id.btn1);        btn.setOnClickListener(this);            }    @Override    public void onClick(View v)     {        showDialog(0);        progressDialog.setProgress(0);                new Thread(new Runnable()        {            @Override            public void run()             {                for (int i = 0; i < 15; i++)                 {                    SystemClock.sleep(5000);                    int temp = (int)(100/15);                    progressDialog.incrementProgressBy(temp);//更新进度条                }                progressDialog.dismiss();                            }                    }).start();                }        @Override    @Deprecated    protected Dialog onCreateDialog(int id)     {        switch (id)         {            case 0:                progressDialog = new ProgressDialog(this);                //return                progressDialog.setIcon(R.drawable.ic_launcher);                                 progressDialog.setTitle((CharSequence)"正在拼命加载中...");                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"ok",                        new DialogInterface.OnClickListener()                         {                            @Override                            public void onClick(DialogInterface dialog, int which)                             {                                Toast.makeText(getBaseContext(), "ok clicked", Toast.LENGTH_LONG).show();                            }                        });                progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"cancel",                        new DialogInterface.OnClickListener()                         {                            @Override                            public void onClick(DialogInterface dialog, int which)                             {                                Toast.makeText(getBaseContext(), "cancel clicked", Toast.LENGTH_LONG).show();                            }                        });                return progressDialog;        default:            break;        }        return null;    }}

image

 

在activity中显示dialog,这里一样用的showDialog(),然后重写onCreateDialog() 方法

onCreateDialog()里面显示一个进度条对话框,然后新线程就负责动态更新它。

progressDialog.dismiss()方法关闭该对话框。

关于Activity的少许细节