android资源:菜单和assets

1、菜单(menu)

1、menu\menu.xml中定义

android资源:菜单和assets
<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    tools:context="com.mytest.testbase.MainActivity" >



    <item

        android:id="@+id/showDialog"

        android:orderInCategory="100"

        android:showAsAction="never"

        android:title="dialog"/>

    

    <item

        android:id="@+id/showToast"

        android:orderInCategory="100"

        android:showAsAction="never"

        android:title="toast"/>    

</menu>
View Code

 

2、activity中代码

android资源:菜单和assets
    @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;

    }



    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

    

        int id = item.getItemId();

        if (id == R.id.showDialog) {

            new AlertDialog.Builder(this).setTitle("测试").setPositiveButton("关闭", new DialogInterface.OnClickListener() {

                

                @Override

                public void onClick(DialogInterface dialog, int which) {

                

                    Toast.makeText(MainActivity.this, "showDialog", Toast.LENGTH_SHORT).show();

                }

            }).show();

        }    if (id == R.id.showToast) {

            Toast.makeText(this, "toast", Toast.LENGTH_SHORT).show();

        }

        return super.onOptionsItemSelected(item);

    }
View Code

 

 

二、assets资源读取
1、在assets中放置文件.

2、activity中读取
InputStream is = getBaseContext().getAssets().open(DBService.DB_NAME);

你可能感兴趣的:(android)