13 January 2017

Android: Handling Click Events of Options Menu - Bắt sự kiện click menu bar menu con (Android Studion) P2

MainActivity.java
Java Android 2017
package com.example.lonely.demostyle;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.setting:
                Toast.makeText(this, "This is setting", Toast.LENGTH_SHORT).show();
                break;
            case R.id.about:
                Toast.makeText(this, "This is about", Toast.LENGTH_SHORT).show();
                break;
            case R.id.facebook:
                Toast.makeText(this, "This is facebook", Toast.LENGTH_SHORT).show();
                break;
            case R.id.yahoo:
                Toast.makeText(this, "This is yahoo", Toast.LENGTH_SHORT).show();
                break;
            case R.id.contact:
                Toast.makeText(this, "This is contact", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
main_menu.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/setting"
        android:title="Setting"
        app:showAsAction="always" />
    <item
        android:id="@+id/about"
        android:title="About"
        app:showAsAction="always" />
    <item
        android:id="@+id/share"
        android:icon="@drawable/ic_action_share"
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/facebook"
                android:title="facebook"
                android:icon="@drawable/ic_facebook"
                app:showAsAction="never" />
            <item
                android:id="@+id/yahoo"
                android:icon="@drawable/ic_yahoo"
                android:title="Yahoo"
                app:showAsAction="never" />
        </menu>

    </item>
    <item
        android:id="@+id/contact"
        android:title="Contact"
        app:showAsAction="never" />
</menu>
activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lonely.demostyle.MainActivity">

    <TextView
        style="@style/Mystyle"
        android:text="@string/hello_world" />
</RelativeLayout>
styles.xml
Java Android 2017
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


    <style name="Mystyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:textColor">#ff0000</item>
        <item name="android:layout_centerHorizontal">true</item>
        <item name="android:textSize">30sp</item>
    </style>

</resources>
strings.xml
Java Android 2017
<resources>
    <string name="app_name">DemoStyle</string>
    <string name="hello_world">Hello World</string>
</resources>

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang