1

我有兩個按鈕多個意圖過濾到多個活動

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 

    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 





    <activity 

     android:name=".Subactivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar" 
     android:taskAffinity="com.example.start_cs.sub"> 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 

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

     > 
    </activity> 

    <activity 

     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar" android:taskAffinity="com.example.start_cs.main" 
     > 

     <intent-filter> 

      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" />   </intent-filter> 


    </activity> 


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

     > 
    </activity> 


</application> 

MainActivity代碼

package com.example.start_cs.myapp; 




Button button; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_main); 
    addListenerOnButton(); 
} 

public void addListenerOnButton() { 

    final Context context = this; 

    button = (Button) findViewById(R.id.main_text_view); 

    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent intent = new Intent(context, main.class); 
      startActivity(intent); 

     } 

    }); 

} 

佈局代碼

相關聯的兩個活動
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/main" 
    android:background="@drawable/main" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/main_text_view" 


    android:layout_marginTop="17dp" 


    /> 
在這種情況下

子活動按鈕打開

但MainActivity按鈕沒有打開主

,但如果我把MainActivity上述子活動

子活動按鈕沒有打開子

但MainActivity按鈕打開主

+0

郵政相關的代碼。清單文件不足以控制按鈕的操作。你的按鈕是你的應用程序的一部分,還是你在談論Android應用程序菜單中的啓動器? – rothloup

+0

按鈕也是我的應用程序@rothloup –

+0

後佈局文件的一部分。 – rothloup

回答

1

好的,根據您對我的評論的回覆,我想我可以說的是,您需要遵循關於如何爲android創建應用的教程。在互聯網上有很多優秀的教程。

你有兩個活動,所以你應該有兩個佈局文件。你只發佈一個。

您還應該有兩個源文件,每個活動一個。你只發佈一個。

您想在代碼中引用的佈局中的每個android元素都需要一個ID。你的代碼引用R.id.main_text_view,但你的佈局文件中沒有這樣的id。我很驚訝你的代碼甚至可以編譯。

然而,回答您的具體問題,你需要的是以下幾點:

1)在你的清單文件<name>標籤必須爲每個活動的Java類的源文件的名稱相匹配。因此,根據您的清單文件,您的活動類文件看起來被稱爲「MainActivity」和「Subactivity」。不過,請在下面查看我對onClickListener代碼的評論。

另外,您的清單表明您的兩項活動都是「發射」活動。您只需要該標籤查看您想要從Android應用程序啓動器啓動的活動(即手機上安裝的所有應用程序的列表)。看起來你只會在你的主要活動中想要這個,但如果你喜歡,你可以指定多個。

2)你的活動是相互對立的(即他們聽起來像他們完全相同的事情 - 每個人都有一個按鈕,啓動另一個),所以代碼將非常相似。您的MainActivity代碼應該是這個樣子:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_main); 
    addListenerOnButton(); 
} 

public void addListenerOnButton() { 

    final Context context = this; 

    button = (Button) findViewById(R.id.main_text_view); //<-- This tries to find a button in this activity (using the activity's layout file that was used in the call to setContentView() in onCreate(). However, the id you specify doesn't exist in your layout file. This should either not compile or return null. 

//This is fine. 
    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent intent = new Intent(context, main.class); //<--"main.class" doesn't match either of the activity names declared in your manifest. It should match one of the names declared in the <name> tag of one of your <activity> tags. 
      startActivity(intent); 

     } 

    }); 

} 

您的佈局文件需要包含一個id爲希望找到使用findViewById()按鈕。按照以下方式修改佈局文件(併爲每個活動創建一個 - 儘管從技術上講,可以爲每個活動引用相同的佈局,但現在,在單獨文件的概念上更容易)。現在

<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/main" 
android:background="@drawable/main" 
android:layout_centerHorizontal="true" 
android:id="@+id/main_text_view" <!-- Here is the line that identifies the button for your app. The format is "@+id/some_name", and is reference as "R.id.some_name" in your code. --> 


android:layout_marginTop="17dp" 


/> 

你必須做同樣的在你的子活動代碼,但你onClickListener將調用的主要活動,而不是你的子活動的。所以onClickListener代碼看起來像這樣爲你MainActivity(這將啓動您的子活動):

button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent intent = new Intent(context, Subactivity.class); 
      startActivity(intent); 

     } 

    }); 

像這樣在你的子活動(這將啓動您的主要活動)

button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent intent = new Intent(context, MainActivity.class); 
      startActivity(intent); 

     } 

    }); 
+0

謝謝你的時間,但我需要告訴你,我有我的所有文件,它的工作得很好,但問題是我怎樣才能使用這兩個按鈕,在同一時間只有一個按鈕,只有工作,如果只有我把它作爲我的清單代碼的第一個,因爲我之前說過,另一個沒有工作的問題只在清單代碼我試過的代碼,它與第一個意圖過濾器很好,如果它MainActivity或Mainsub @rothloup「我擁有所有我的xml和java文件「 –

+0

@mmorsy清單不是代碼。它不確定你的按鈕的行爲。所有它(在你的情況)是聲明你的應用程序的組件(活動,接收者,服務等)。僅僅因爲您在清單中交換訂單並且獲得不同的行爲並不意味着解決方案在於清單。如果你發佈了所有的文件,那麼你錯過了很多代碼和佈局,這些都是你描述所需要的。我希望我的回答至少能幫助你指出正確的方向。 – rothloup

+0

我的檔案http://pasted.co/5834e7be - http://pasted.co/8afb9f92 - http://pasted.co/b82bcf8a - http://pasted.co/75bbd1b0 - http://pasted.co/0b791d98 - http://pasted.co/bf2ec691 - http://pasted.co/26db9cad - http://pasted.co/8adef098 @rothloup –

相關問題