当前位置:操作系统 > 安卓/Android >>

Android新手入门教程(五):理解Activityの不得不说的Context

在开发Android的过程中,总是能遇见Context类或者它的实例。Context类的实例经常被用来提供“应用程序”的引用。例如下面的代码片段,Toast类的第一个参数接受一个Context对象:


[java]
@Override 
    protected Dialog onCreateDialog(int id) { 
        switch (id) { 
        case 0: 
 
            Builder builder = new AlertDialog.Builder(this); 
            builder.setIcon(R.drawable.ic_launcher); 
            builder.setTitle("This is a dialog with some 易做图 text..."); 
            builder.setPositiveButton("OK", 
                    new DialogInte易做图ce.OnClickListener() { 
                        public void onClick(DialogInte易做图ce dialog, 
                                int whichButton) { 
                            Toast.makeText(getBaseContext(), "OK clicked!", 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
 
            builder.setNegativeButton("Cancel", 
                    new DialogInte易做图ce.OnClickListener() { 
                        public void onClick(DialogInte易做图ce dialog, 
                                int whichButton) { 
                            Toast.makeText(getBaseContext(), "Cancel clicked!", 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
 
            builder.setMultiChoiceItems(items, itemsChecked, 
                    new DialogInte易做图ce.OnMultiChoiceClickListener() { 
                        public void onClick(DialogInte易做图ce dialog, int which, 
                                boolean isChecked) { 
                            Toast.makeText( 
                                    getBaseContext(), 
                                    items[which] 
                                            + (isChecked ? " checked!" 
                                                    : " unchecked!"), 
                                    Toast.LENGTH_SHORT).show(); 
                        } 
                    }); 
            return builder.create(); 
 
        } 
        return null; 
    } 
@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:

            Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(R.drawable.ic_launcher);
            builder.setTitle("This is a dialog with some 易做图 text...");
            builder.setPositiveButton("OK",
                    new DialogInte易做图ce.OnClickListener() {
                        public void onClick(DialogInte易做图ce dialog,
                                int whichButton) {
        &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,