2011-12-15 8 views
2

私はすべてのアクティビティでいくつかのコードを実装しようとしており、コードをコピーして各アクティビティにページしたくありません。 インターフェイスを使用してすべてのアクティビティでコードを実装する

は、もともと私はその後、他のすべてを延長コードで親アクティビティを持っていたが、私はListActivitiesまたはExpandableListActivitiesでこれを行うことができませんでした。

これはインターフェイスクラスを使用し、各アクティビティにこれを実装させることで実現されると思います。しかし、私がこの試みをしようとすると、私にエラーが表示され、メソッド本体が削除されます。ここで

は、私はJavaでこれまで

import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.Toast; 

public interface MenuOptions { 

    /** 
    * Method called when the hardware menu button is called. Uses optionmenu.xml for layout 
    */ 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.optionmenu, menu); 
     return true; 
    } 

    /** 
    * Event listener for the options menu. If home is pressed user is sent to home screen. If settings is pressed user is sent to setting screen 
    * User is passed as an extra 
    */ 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     Intent nextIntent = null; 

     switch (item.getItemId()) { 
      case R.id.home:  
         Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show(); 
         nextIntent = new Intent(this, Home.class); 

         break; 
      case R.id.settings:  
         Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show(); 
         nextIntent = new Intent(this, Settings.class); 
         break; 

     } 

     nextIntent.putExtra("user", user); 
     startActivity(nextIntent); 
     return true; 
    } 

} 
+0

すてきな質問、私は興味深い答えを知りたいです。 –

答えて

2

Interfaceクラスメソッドのシグネチャなしの実装をのみ含まれている必要があります持っているものです。したがって、あなたは、基本クラスを作成する必要があります。

public class MenuOptions extends Activity { 
    /** 
    * Method called when the hardware menu button is called. Uses optionmenu.xml for layout 
    */ 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.optionmenu, menu); 
     return true; 
    } 

    /** 
    * Event listener for the options menu. If home is pressed user is sent to home screen. If settings is pressed user is sent to setting screen 
    * User is passed as an extra 
    */ 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     Intent nextIntent = null; 

     switch (item.getItemId()) { 
      case R.id.home:  
         Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show(); 
         nextIntent = new Intent(this, Home.class); 

         break; 
      case R.id.settings:  
         Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show(); 
         nextIntent = new Intent(this, Settings.class); 
         break; 

     } 

     nextIntent.putExtra("user", user); 
     startActivity(nextIntent); 
     return true; 
    } 
} 

そして、あなたの活動:

public class YourActivity extends MenuOptions { 
/*...*/ 
} 
+0

こんにちは、あなたの応答のおかげで、このソリューションはまだ私はほぼ正確にアクティビティ、ListActivitiesとExplandableListActivitiesのための同じコードで3つのアクティビティを持っていることを意味します。 – TrueWheel

0

インターフェースはメソッド本体を持つことが許可されていません。これは、オブジェクトのインターフェイスを記述し、メソッド自体を記述するものではないためです。

すべてのアクティビティで共有される同じメニューオプションの問題は共通しています。これを処理する最も簡単な方法は、2つまたは3つのスーパークラス(ListActivitiesおよびMapActivities用)を定義することです。

+0

こんにちは、お返事ありがとうございます。これはちょうど非常にclunkyと感じる答えかもしれないように見えます。 – TrueWheel

0

私が知る限り、インターフェイスは、インターフェイスを実装しているクラスによってサポートされているメソッドのみを記述しています。 これは、インターフェイスの実装とクラスの拡張の違いです。 - すべてのクラスで使用されているインターフェイスを作成することはできません。そのインターフェイスを実装しているクラスによって定義されるメソッドのセットのみを定義してください。

私はあなたの最善の策は、同じ仕事をするためにあなたのすべての活動によって使用することができます。次に、各アクティビティによって作成され、初期化されるそのクラスのインスタンスが必要になります。onCreate(または他の場所)

新しいクラスを静的にすることができますので、インスタンスを作成する必要はありません。ちょうどそれを行う一つの他の方法は、標準的な動作を含んでいる活動のサブクラスを作成し、このサブクラスのように、すべてのあなたの活動を作成することで、右の回で

の静的メソッドを呼び出します。 ListActivitiesを持っているところでは、リストを含むサブクラス化されたアクティビティを作成して、いくつかのレコーディングを行う必要があります。

関連する問題