android 常用代码

1.在测试时,如何实现一个提示

Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show(); 
//从资源文件string.xml 里面取提示信息
Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_SHORT).show();
//这个提示会几秒钟后消失
如果this报错的话,就把this换成MainActivity.this

 

 

2.定义一个按钮,并设置事件

ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
        ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                exitApp();
            }
        });

3.捕获返回键

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && isSaved) {
            try {
                Intent intent = new Intent(this, ContactActivity.class);
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this, "出错啦!不好意思", Toast.LENGTH_SHORT).show(); 
            }
        }
        return super.onKeyDown(keyCode, event);
    }

 4. 退出时询问

Dialog dialog = new AlertDialog.Builder(this).setIcon(R.drawable.pic_m)
                .setTitle("程序退出? ").setMessage("您确定要退出本程序吗?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MyActivityGroupDemo.this.finish() ;
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MyActivityGroupDemo.this.switchActivity(0);
                    }
                }).create();

        dialog.show();

 5.preference.xml 背景颜色,以及滑动时是黑色,如何修改?

this.getListView().setBackgroundColor(Color.argb(150, 247, 247, 247));
this.getListView().setCacheColorHint(Color.TRANSPARENT);

 6.preference.xml 启动intent

<PreferenceScreen
        android:key="codecs"
        android:title="@string/codecs" >
            <intent android:action="android.intent.action.MAIN"
                android:targetPackage="org.sipdroid.sipua"
                android:targetClass="org.sipdroid.codecs.Codecs$CodecSettings" />

    </PreferenceScreen>

 

 

 

posted @ 2012-09-11 08:11  loverszhaokai  阅读(175)  评论(0编辑  收藏  举报