2016-04-29 77 views
1

我在我的应用程序中设置了工具栏(第一次),但我无法将主页(/向上)按钮设置为单击时执行任何操作。 我有3个活动(主动活动,第二,第三),我已经定义了所有3的工具栏,但没有任何反应,当我点击每个活动炎的主页按钮时,我希望按钮做他的(在我的理解)默认行动这是回到HomeActivity,这是“MainActivity”。工具栏中的向上按钮不会执行任何操作

下面是一些相关的代码: 工具栏布局(命名为:app_bar.xml):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/toolBar_background" 
     android:elevation="5dp" 
     android:title="@string/app_name" /> 
</RelativeLayout> 

我的3个活动布局个XML: activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<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:orientation="vertical" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.example.noamm_000.finaltoolbarproject.MainActivity"> 

    <include layout="@layout/app_bar"/> 
//More layout code... 

Activity_Second.xml:

<?xml version="1.0" encoding="utf-8"?> 
<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" 

    tools:context="com.example.noamm_000.finaltoolbarproject.Second"> 

    <include layout="@layout/app_bar"/> 
//More layout code... 

Activity_Third.xml:

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    tools:context="com.example.noamm_000.finaltoolbarproject.Third"> 

    <include layout="@layout/app_bar"/> 
//More layout code 

实际上所有的3个活动都只包含名为“app_bar.xml”的工具栏布局。

这是我的Mainfest文件的一部分,其中我配置app_parent:

<activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Second" 
      android:parentActivityName=".MainActivity"> 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value=".MainActivity"/> 
      <intent-filter> 
       <action android:name="com.example.noamm_000.finaltoolbarproject.Second" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Third" 
      android:parentActivityName=".MainActivity"> 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value=".MainActivity"/> 
      <intent-filter> 
       <action android:name="com.example.noamm_000.finaltoolbarproject.Third" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

和ofcourse的我的每个3个活动我的Java代码部分: MainActivity:

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

     toolbar = (Toolbar) findViewById(R.id.toolbar_id); 
     setSupportActionBar(toolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); 

     mainBtn = (Button) findViewById(R.id.btn_main); 
     openSecondActivity(); 
    } 
    public void openSecondActivity(){ 
     mainBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent("com.example.noamm_000.finaltoolbarproject.Second"); 
       startActivity(intent); 
      } 
     }); 

SecondActivity:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 
     btnSecond = (Button) findViewById(R.id.btn_second); 

     toolbar = (Toolbar) findViewById(R.id.toolbar_id); 
     setSupportActionBar(toolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); 

     openThirdActivity(); 
    } 

    public void openThirdActivity(){ 
     btnSecond.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent("com.example.noamm_000.finaltoolbarproject.Third"); 
       startActivity(intent); 
      } 
     }); 
    } 

and ThirdActivity:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_third); 

     toolbar = (Toolbar) findViewById(R.id.toolbar_id); 
     setSupportActionBar(toolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); 
    } 

    public boolean onCreateOptionsMenu(Menu menu){ 
     getMenuInflater().inflate(R.menu.toolbar_menu,menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

你可以看到,所有3个活动“的onCreate”功能几乎相同.. 当我开始我的应用程序,我可以看到在所有3个活动的工具栏,我可以看到后面的箭头,但在所有点击它什么也不做。 我知道我可以设置toolbar.setNavigationOnClickListener但我只是想它,使其默认操作是去家活动.... 非常感谢你, 诺姆

回答

0

尝试像这样...

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == android.R.id.home) { 
      Intent i = new Intent(CurrentActivity.this, HomeActivity.class); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(i); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
+0

谢谢你,它的工作,但不存在这个按钮defaullt函数,我不必定义使用onOptionsItemSelected函数? – Noam

+0

@ Noam ..它是这个按钮的默认功能..你必须定义它,如果你想在工具栏中的向上按钮必须做一些你的定义特定的任务... –

+0

@Noam我的代码将带你到父活动,而不仅仅是通过后面的堆栈。我还添加了Intent.Flag来清除后退堆栈,这是一个非常有用的功能,当您开始家庭活动时,当用户使用'Up'按钮时可以阻止后退堆栈进入混乱状态 –

0

试试这个:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       //finish(); or Do wahtever you wnat to do 
       return true; 
    } 
    return true; 
} 
+0

检查[this](http://developer.android.com/training/implementing-navigation/ancestral。html#NavigateUp) – Raghavendra

+0

@Noam waht错误? – Raghavendra

+0

谢谢,它的工作,但不存在这个按钮defaullt函数,我不必定义使用onOptionsItemSelected函数? – Noam

0

如果您有父活动,按照此代码,

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()){ 
     case android.R.id.home: 
      if(NavUtils.getParentActivityIntent(this)== null) { 
       onBackPressed(); 
      }else{ 
       NavUtils.navigateUpFromSameTask(this); 
      } 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

谢谢,它的工作,但不存在这个按钮defaullt函数,我不必定义使用onOptionsItemSelected函数? – Noam

0

你们的活动

@Override 
public boolean onOptionsItemIdSelected(int id) { 
    switch (id) { 
      case R.id.home: 
       //do stuff 
       break; 
    } 
    return true; 
} 

在你的情况试试这个,我建议您创建继承所有的活动AbstractActivity。因此,您可以考虑许多行为,包括管理工具栏按钮

+0

它应该是Android.R.id.home(没有adnroid词 - 没有任何反应),它的工作,但不存在这个按钮defaullt函数,我不必定义使用onOptionsItemSelected函数?和关于你的建议 - 听起来不错,将尝试 – Noam

+0

这是整个ToolbarButton的默认功能:) –

相关问题