2016-09-14 75 views
0

在我的項目中,我只有一個ActivityView。 我認爲它有兩個View切換View。第一個View是我的家,有一個名爲「play」的Button。當您點擊播放Button進入第二個View。第二View是我的遊戲。 現在我的問題是,當我想在第二個View中使用onBackPressed()方法時,它會關閉Activity。和onBackPressed()方法在View中都一樣。 如何處理onBackPressed()方法在第二個View返回到第一個View。 如何在onBackPressed()中切換View? 我是新的Android,現在我真的很困惑。 有什麼建議嗎?或任何關鍵字來搜索解決我的問題。onBackPressed()完成我的活動

這裏是我的代碼:

public class PTPlayer extends Cocos2dxActivity { 

static Splash splash; 
public static AppList appList; 
static Noti_Queue noti_queue; 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 


    Log.v("----------", "onActivityResult: request: " + requestCode + " result: " + resultCode); 

    if (requestCode == PTServicesBridge.RC_SIGN_IN) { 
    } 
} 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 


    if (splash == null) { 
     splash = new Splash(this); 
     splash.set_identity("1"); 
    } 


    if (appList == null) { 
     appList = new AppList(this); 
     appList.set_identity("1"); 
    } 


    if (noti_queue == null) { 
     noti_queue = new Noti_Queue(this); 
     noti_queue.set_identity("1"); 
    } 


} 

@Override 
public void onNativeInit() { 
    initBridges(); 
} 

private void initBridges() { 
    PTStoreBridge.initBridge(this); 
    PTServicesBridge.initBridge(this, getString(R.string.app_id)); 

    if (PTJniHelper.isAdNetworkActive("kChartboost")) { 
     PTAdChartboostBridge.initBridge(this); 
    } 

    if (PTJniHelper.isAdNetworkActive("kRevMob")) { 
     PTAdRevMobBridge.initBridge(this); 
    } 

    if (PTJniHelper.isAdNetworkActive("kAdMob") || PTJniHelper.isAdNetworkActive("kFacebook")) { 
     PTAdAdMobBridge.initBridge(this); 
    } 

    if (PTJniHelper.isAdNetworkActive("kAppLovin")) { 
     PTAdAppLovinBridge.initBridge(this); 
    } 

    if (PTJniHelper.isAdNetworkActive("kLeadBolt")) { 
     PTAdLeadBoltBridge.initBridge(this); 
    } 

    if (PTJniHelper.isAdNetworkActive("kVungle")) { 
     PTAdVungleBridge.initBridge(this); 
    } 

    if (PTJniHelper.isAdNetworkActive("kPlayhaven")) { 
     PTAdUpsightBridge.initBridge(this); 
    } 

    if (PTJniHelper.isAdNetworkActive("kMoPub")) { 
     PTAdMoPubBridge.initBridge(this); 
    } 

    if (PTJniHelper.isAdNetworkActive("kFacebook")) { 
     PTAdFacebookBridge.initBridge(this); 
    } 

    if (PTJniHelper.isAdNetworkActive("kHeyzap")) { 
     PTAdHeyzapBridge.initBridge(this); 
    } 

} 

@Override 
public Cocos2dxGLSurfaceView onCreateView() { 
    Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this); 
    glSurfaceView.setEGLConfigChooser(8, 8, 8, 0, 0, 0); 

    return glSurfaceView; 
} 

static { 
    System.loadLibrary("player"); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if (PTJniHelper.isAdNetworkActive("kChartboost")) { 
     PTAdChartboostBridge.onResume(this); 
    } 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    if (PTJniHelper.isAdNetworkActive("kChartboost")) { 
     PTAdChartboostBridge.onStart(this); 
    } 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    if (PTJniHelper.isAdNetworkActive("kChartboost")) { 
     PTAdChartboostBridge.onStop(this); 
    } 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
} 


@Override 
public void onBackPressed() { 
    splash.Display(); 
    splash = null; 
    super.onBackPressed(); 
} 
} 

在這裏,我想,在我的第二個觀點:

 public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener { 
// =========================================================== 
// Constants 
// =========================================================== 

private static final String TAG = Cocos2dxActivity.class.getSimpleName(); 

// =========================================================== 
// Fields 
// =========================================================== 

private Cocos2dxGLSurfaceView mGLSurfaceView; 
private Cocos2dxHandler mHandler; 
private static Context sContext = null; 

public static Context getContext() { 
    return sContext; 
} 

// =========================================================== 
// Constructors 
// =========================================================== 

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    sContext = this; 
    this.mHandler = new Cocos2dxHandler(this); 

    this.init(); 

    Cocos2dxHelper.init(this, this); 


} 

// =========================================================== 
// Getter & Setter 
// =========================================================== 

// =========================================================== 
// Methods for/from SuperClass/Interfaces 
// =========================================================== 

@Override 
protected void onResume() { 
    super.onResume(); 

    Cocos2dxHelper.onResume(); 
    this.mGLSurfaceView.onResume(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 

    Cocos2dxHelper.onPause(); 
    this.mGLSurfaceView.onPause(); 
} 

@Override 
public void showDialog(final String pTitle, final String pMessage) { 
    Message msg = new Message(); 
    msg.what = Cocos2dxHandler.HANDLER_SHOW_DIALOG; 
    msg.obj = new Cocos2dxHandler.DialogMessage(pTitle, pMessage); 
    this.mHandler.sendMessage(msg); 
} 

@Override 
public void showEditTextDialog(final String pTitle, final String pContent, final int pInputMode, final int pInputFlag, final int pReturnType, final int pMaxLength) { 
    Message msg = new Message(); 
    msg.what = Cocos2dxHandler.HANDLER_SHOW_EDITBOX_DIALOG; 
    msg.obj = new Cocos2dxHandler.EditBoxMessage(pTitle, pContent, pInputMode, pInputFlag, pReturnType, pMaxLength); 
    this.mHandler.sendMessage(msg); 
} 

@Override 
public void runOnGLThread(final Runnable pRunnable) { 
    this.mGLSurfaceView.queueEvent(pRunnable); 
} 

// =========================================================== 
// Methods 
// =========================================================== 
public void init() { 

    // FrameLayout 
    ViewGroup.LayoutParams framelayout_params = 
     new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
            ViewGroup.LayoutParams.FILL_PARENT); 
    FrameLayout framelayout = new FrameLayout(this); 
    framelayout.setLayoutParams(framelayout_params); 

    // Cocos2dxEditText layout 
    ViewGroup.LayoutParams edittext_layout_params = 
     new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT); 




    this.mGLSurfaceView = this.onCreateView(); 



    // Switch to supported OpenGL (ARGB888) mode on emulator 
    if (isAndroidEmulator()) 
     this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0); 

    this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer()); 


      RelativeLayout relativeLayout = new   RelativeLayout(getApplicationContext()); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
    relativeLayout.setLayoutParams(params); 

    //AdView adad = new AdView(this); 
    ClickBanner_CLickYab_Holder adad = new ClickBanner_CLickYab_Holder(this); 
    RelativeLayout.LayoutParams adad_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    adad_params.addRule(RelativeLayout.CENTER_HORIZONTAL); 
    adad_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    // adad.setToken(getString(R.string.adad_token)); 
    adad.setLayoutParams(adad_params); 

    Button myButton = new Button(this); 


    myButton.setBackgroundResource(R.drawable.more); 
    RelativeLayout.LayoutParams adad_params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 

      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    adad_params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    adad_params1.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
    myButton.setLayoutParams(adad_params1); 
    myButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      PTPlayer.appList.Display(); 
     } 
    }); 

    Button myButton1 = new Button(this); 


    myButton1.setBackgroundResource(R.drawable.more); 
    RelativeLayout.LayoutParams adad_params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 

      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    adad_params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    adad_params2.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
    myButton1.setLayoutParams(adad_params2); 
    myButton1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      PTPlayer.appList.Display(); 
     } 
    }); 


    relativeLayout.addView(this.mGLSurfaceView); 
    relativeLayout.addView(adad); 
    relativeLayout.addView(myButton); 
    relativeLayout.addView(myButton1); 
    ClickBanner_CLickYab_Holder.setTestMode(); 
    setContentView(relativeLayout); 




} 

public Cocos2dxGLSurfaceView onCreateView() { 
    return new Cocos2dxGLSurfaceView(this); 
} 

    private final static boolean isAndroidEmulator() { 
    String model = Build.MODEL; 
    Log.d(TAG, "model=" + model); 
    String product = Build.PRODUCT; 
    Log.d(TAG, "product=" + product); 
    boolean isEmulator = false; 
    if (product != null) { 
    isEmulator = product.equals("sdk") || product.contains("_sdk") ||  product.contains("sdk_"); 
    } 
    Log.d(TAG, "isEmulator=" + isEmulator); 
    return isEmulator; 
} 
} 
+0

什麼你做想要做到嗎? –

+0

@AnshulTyagi我想當我使用onbackpresed第二個視圖去第一個視圖 – nsr

回答

0

重寫onBackPressed()的活動,並在那裏你想要去提供你的屏幕。 onBackpressed()檢查您正在顯示的當前視圖,並根據移至第一個視圖。

+0

怎麼樣?你能解釋更多 – nsr

+0

@nsr檢查編輯答案 –

+0

我知道它但我不知道如何使用它在我的代碼 – nsr

0

只是不要每次撥打super.onBackPressed()

@Override 
    public void onBackPressed() { 
     if (isFirstView()) { 
      super.onBackPressed(); 
     } else { 
      switchToFirstView(); 
     } 

只有當沒有最後一個可用視圖時才能調用。或者你想關閉應用程序的地方。當您進行第一項活動時,該代碼將完成您的活動。如果您正在進行第二項活動,請切換到第一項活動。 只需根據您的代碼替換我的方法。

+0

什麼是islastview()和swtichtopreviuosview()? – nsr

+0

只給了你切換視圖的邏輯。您至少自己切換視圖。謝謝。 – Mann

+0

我的問題是,究竟如何切換到下一個視圖 – nsr

0

然後做到這一點。

@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
    Intent intent = new Intent(MainActivityPhase2.this, GlobalSearch.class); 
       startActivity(intent); 
    finish(); 
} 
3

,如果你想留在currnt活動中使用這樣的

@Override 
    public void onBackPressed() { 
     return; 
    } 

,如果你想使用雙擊退出則必須使用覆蓋方法爲當後退按鈕按下 和一個點擊入住期間,您可以使用這樣

首先定義一個變量雙擊

boolean doubleBackToExit = false; 

和覆蓋後退按鈕方法

@Override 
    public void onBackPressed() { 
     if (doubleBackToExit) { 
     //on double back button pressed 
      return; 
     } 
     this.doubleBackToExit = true; 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       doubleBackToExit=false; 
      } 
     }, 2000); 
    } 
+0

我不希望這個 – nsr

0
在第二類Cocos2dxActivity

,把這個代碼。

@Override 
    public void onBackPressed() { 
     this.finish(); 
     } 
0

如果你有兩個View只是一個活動,你可以使用Fragments。 使用片段,Activity.OnBackPressed()將刪除堆棧中的最後一個片段,並且可以解決您的問題。

所以,在活動中,你必須把容器在XML佈局文件:

<FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:clickable="true" android:layout_height="match_parent"/>

在活動的java文件:

getFragmentManager().beginTransaction() .add(R.id.container,new YourHomeFragment()) .commit();

所以要加第二你可以使用這段代碼片段:

注意:您可以調用此代碼或在YourHomeFragment類(按鈕clickListener)或您的活動(使用回調系統)。例如:

在YourHomeFragment - > playButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getFragmentManager().beginTransaction() .add(R.id.container,new YourPlayFragment()) .addToBackStack("YourPlayFragment") //string what you want .commit(); } });

這樣,你必須聲明兩個佈局xml文件的碎片和一個活動。

名單的Java和相對XML文件:

MainActivity.java activity_main.xml

YourHomeFragment.java fragment_your_home.xml < - 在這裏插入你的第一個視圖

YourPlayFragment.java fragment_your_play.xml < - 玩視圖

+0

我的活動沒有任何佈局 – nsr

+0

好吧。您可以嘗試使用init()方法中聲明的FrameLayout。 getFragmentManager()。beginTransaction() .add(framelayout.getId(),new YourHomeFragment()) .commit();' – hooloovoo