82

Tôi muốn tạo mục đích bắt đầu một hoạt động mới sau khi nhấp vào Mục menu, nhưng tôi không chắc chắn cách thực hiện việc này. Tôi đã đọc qua tài liệu android, nhưng việc triển khai của tôi không đúng ... và một số hướng dẫn đúng hướng sẽ giúp ích. Tôi đã liệt kê mã của tôi dưới đây và nhận xét ra các vấn đề của tôi, tôi nghĩ tôi đang gọi phương thức sai.Xử lý mục menu Nhấp vào Sự kiện - Android

package com.jbsoft.SimpleFlashlight; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.*; 
import android.view.MenuItem.OnMenuItemClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

public class SimpleFlashLightActivity extends Activity { 


    Button GreenButton; // Declare instances of buttons to use later 
    Button BlueButton; 

    private static final int OK_MENU_ITEM = Menu.FIRST; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    BlueButton = (Button) findViewById(R.id.bluebutton); 
    BlueButton.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

     //Display msg when user clicks Blue Button 
     showColorChangeMsg(); 

     // Switch Activities on click 
     Intent blueintent = new Intent(SimpleFlashLightActivity.this, 
             BlueFlashLightActivity.class); 
     startActivity(blueintent); 

     } 
    }); 
    //Install listener for second button 
    GreenButton = (Button) findViewById(R.id.greenbutton); 
    GreenButton.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

     // Display msg when user clicks Green Button 
     showColorChangeMsg(); 

     Intent greenintent = new  Intent(SimpleFlashLightActivity.this, 
               GreenFlashLightActivty.class); 
     startActivity(greenintent); 

     } 
    }); 

    ; 

    /**************************************************************************************/ 

    // Method Declarations // THIS IS WHERE I'M HAVING A PROBLEM 

    MenuItem AddColorButton = (MenuItem)findViewById(R.id.menu_insert); 

    boolean onOptionsItemSelected(AddColorButton) { 
     Intent intent = new Intent(SimpleFlashLightActivity.this, 
            BlueFlashLightActivity.class); 
     startActivity(intent); 
     return true; 
     ; 
    }; 
    /****************************************************************************************/ 

    } 
    private void showColorChangeMsg() 
    { 
    Toast msgtoast = Toast.makeText(this.getBaseContext(), "SWITCH COLOR!", 
            Toast.LENGTH_LONG); 
    msgtoast.show(); 
    } 
    private void showMsg(String msg) { 
    Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG); 
    toast.show(); 
    } 

    public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 
    MenuInflater mi = getMenuInflater(); 
    mi.inflate(R.menu.list_menu, menu); 
    return true; 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case OK_MENU_ITEM: 
     showMsg("OK"); 
     break; 
    } 
    return super.onOptionsItemSelected(item); 
    } 

} 

Trả lời

220

mã đơn giản để tạo trình đơn ..

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

mã đơn giản cho menu đã chọn

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 
    case R.id.new_game: 
     newGame(); 
     return true; 
    case R.id.help: 
     showHelp(); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 

cho cụ thể hơn hãy bên dưới liên kết ..

Link1

Link2

+0

Liệu các android: onClick thuộc tính không hoạt động trong trường hợp này nếu tôi được đưa rằng trong XML? (Lập trình Android rất mới bắt đầu ở đây) – FateNuller

+0

@FateNuller onClick bên trong XML cho trình đơn tùy chọn sẽ không hoạt động, nó sẽ hoạt động đối với bố cục. Bạn nên xử lý các nhấp chuột vào thanh tác vụ tùy chọn bên trong onOptionsItemSelected. – Marko

+0

Đơn giản và tỉnh táo ... !!! –

4

menu mục tập tin trông giống như

res/menu/menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity"> 
    <item 
     android:id="@+id/settings" 
     android:title="Setting" 
     app:showAsAction="never" /> 
    <item 
     android:id="@+id/my_activity" 
     android:title="My Activity" 
     app:showAsAction="always" 
     android:icon="@android:drawable/btn_radio"/> 
</menu> 

mã Java trông giống như

src/MainActivity.java

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     if (id == R.id.my_activity) { 
      Intent intent1 = new Intent(this,MyActivity.class); 
      this.startActivity(intent1); 
      return true; 
     } 

     if (id == R.id.settings) { 
      Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show(); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

Và thêm đoạn mã sau vào tệp AndroidManifest.xml của bạn

<activity 
      android:name=".MyActivity" 
      android:label="@string/app_name" > 
     </activity> 

Tôi hy vọng nó sẽ giúp bạn.

6

Add Sau Mã

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.new_item: 
     Intent i = new Intent(this,SecondActivity.class); 
      this.startActivity(i); 
      return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 
1

Mã này là công việc đối với tôi

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 

    if (id == R.id.action_settings) { 
    // add your action here that you want 
     return true; 
    } 

    else if (id==R.id.login) 
    { 
     // add your action here that you want 
    } 


    return super.onOptionsItemSelected(item); 
} 
Các vấn đề liên quan