본문 바로가기

Android

메뉴 - context

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

    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=".MainActivity" >

 

    <Button

        android:id="@+id/btn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="컨택스트 메뉴" />

 

</RelativeLayout>

 

 

package com.example.contextex01;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.view.ContextMenu;

import android.view.ContextMenu.ContextMenuInfo;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

           Button btn = (Button)findViewById(R.id.btn);

           registerForContextMenu(btn);

     }

 

     @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 onContextItemSelected(MenuItem item) {

           // TODO Auto-generated method stub

           switch (item.getItemId()) {

           case 1:

                Toast.makeText(this, "짜장", Toast.LENGTH_SHORT).show();

                break;

           case 2:

                Toast.makeText(this, "짬뽕", Toast.LENGTH_SHORT).show();

                break;

           case 3:

                Toast.makeText(this, "우동", Toast.LENGTH_SHORT).show();

                break;

    

           default:

                break;

           }

           return super.onContextItemSelected(item);

     }

 

     @Override

     public void onCreateContextMenu(ContextMenu menu, View v,

                ContextMenuInfo menuInfo) {

           // TODO Auto-generated method stub

           super.onCreateContextMenu(menu, v, menuInfo);

          

           menu.add(0, 1, 0, "짜장");

           menu.add(0, 2, 0, "짬뽕");

           menu.add(0, 3, 0, "우동");

     }

}

 



 

'Android' 카테고리의 다른 글

리스트뷰 - choiceMode  (0) 2013.04.01
리스트뷰 - 어댑터뷰  (0) 2013.03.29
메뉴 - 체크메뉴  (0) 2013.03.29
메뉴(menu) - 옵션 메뉴  (0) 2013.03.29
셰이더 - ShapeDrawable  (0) 2013.03.29