2014-01-21 113 views
-1

我一直在努力讓用戶在菜單上點擊後改變背景顏色,但我不明白爲什麼它不起作用。我沒有任何警告或錯誤!Android上的操作Android不起作用

我activity_main.xml中:

<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:id="@+id/utilayout" 
tools:context=".MainActivity" 
> 

<TextView 
    android:id="@+id/hello1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/hello1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="80dp" 
    android:src="@drawable/ic_launcher" android:contentDescription="@string/hello_world"/> 

我MainActivity.java:

package com.example.menu; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 


public class MainActivity extends Activity { 

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


@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; 
} 

public boolean onOptionsItemsSelected(MenuItem item) 
{ 
    RelativeLayout bkgr = (RelativeLayout) findViewById(R.id.utilayout); 

    switch(item.getItemId()) 
    { 
    case R.id.action_black: 
     bkgr.setBackgroundResource(R.color.black); 
     return true; 

    case R.id.action_red: 
     bkgr.setBackgroundResource(R.color.red); 
     return true; 

    case R.id.action_hello: 
     TextView tv = (TextView) findViewById(R.id.hello1); 
     tv.setText("What up people?"); 
     return true; 

    default: return super.onOptionsItemSelected(item); 
    } 
} 
} 

的main.xml(菜單)

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


    <item 
    android:id="@+id/action_red" 
    android:orderInCategory="100" 
    android:showAsAction="never" 
    android:title="@string/action_red"/> 
    <item 
    android:id="@+id/action_black" 
    android:orderInCategory="200" 
    android:showAsAction="never" 
    android:title="@string/action_black"/> 
    <item 
    android:id="@+id/action_hello" 
    android:orderInCategory="300" 
    android:showAsAction="never" 
    android:title="@string/hello"/> 

回答

0

與您現有的一個替換此代碼。和你的應用程序將運行良好

MainActivity.java:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    RelativeLayout bkgr = (RelativeLayout) findViewById(R.id.utilayout); 
    switch (item.getItemId()) { 
     case R.id.action_black: 
      bkgr.setBackgroundResource(R.color.black); 
      return true; 
     case R.id.action_red: 
      bkgr.setBackgroundResource(R.color.red); 
      return true; 
     case R.id.action_hello: 
      TextView tv = (TextView) findViewById(R.id.hello1); 
      tv.setText("What up people?"); 
      return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

的main.xml(菜單)

<item 
     android:id="@+id/action_red" 
     android:orderInCategory="100" 
     android:showAsAction="always" 
     android:title="red action"/> 
<item 
     android:id="@+id/action_black" 
     android:orderInCategory="200" 
     android:showAsAction="always" 
     android:title="black action"/> 
<item 
     android:id="@+id/action_hello" 
     android:orderInCategory="300" 
     android:showAsAction="always" 
     android:title="hello"/> 
</menu> 
+0

不起作用!當我點擊紅色時,背景顏色不會變成紅色。點擊菜單中的任何選項後沒有真正發生! – wisejoy

+0

您是否在onOptionsItemSelected()方法之前嘗試了@Override? – Zia

+0

是的。我不知道那個項目有什麼問題。我嘗試在新項目中重新編譯類似的代碼,它工作正常。無論如何非常感謝。 – wisejoy

0

更改 '迴歸真實' 到 '休息'
編輯:

switch(item.getItemId()) 
{ 
    case R.id.action_black: 
    bkgr.setBackgroundResource(R.color.black); 
    break; 

case R.id.action_red: 
    bkgr.setBackgroundResource(R.color.red); 
    break; 

case R.id.action_hello: 
    TextView tv = (TextView) findViewById(R.id.hello1); 
    tv.setText("What up people?"); 
    break; 

default: 
break; 
return super.onOptionsItemSelected(item); 
} 
+0

做到這一點,但該方法有一個布爾返回類型。所以我添加了默認語法的返回類型。點擊菜單中的任何選項後,仍然沒有收到應用程序的回覆! – wisejoy