工具栏中的向上按钮不执行任何操作
Up Button in Toolbar doesn't do anything
我在我的应用程序中设置了工具栏(这是第一次),但是我无法将主页 (/Up) 按钮设置为在单击时执行任何操作。
我有 3 个活动(MainActivity、Second、Third)并且我已经在所有 3 个中定义了工具栏但是当我在每个活动中单击主页按钮时没有任何反应我希望按钮执行他的操作(在我的理解)返回首页的默认操作 Activity 即 "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>
当然还有我的 java 代码的一部分,用于我的 3 个活动中的每一个:
主要Activity:
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);
}
});
第二Activity:
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);
}
});
}
和第三Activity:
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 个 Activity "onCreate" 功能几乎相同..
当我启动我的应用程序时,我可以在所有 3 个活动中看到工具栏,我可以在所有活动中看到后退箭头,但单击它什么都不做。
我知道我可以设置 toolbar.setNavigationOnClickListener
但我只是想让它成为默认操作,即回家 activity...
非常感谢你,
诺姆
试试这个:
@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;
}
这样试试...
@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);
}
如果您有 parent activity,请遵循此代码,
@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);
}
在你的活动中试试这个
@Override
public boolean onOptionsItemIdSelected(int id) {
switch (id) {
case R.id.home:
//do stuff
break;
}
return true;
}
对于您的情况,我建议您创建一个继承所有活动的 AbstractActivity。所以你可以考虑一些行为,包括工具栏按钮的管理
我在我的应用程序中设置了工具栏(这是第一次),但是我无法将主页 (/Up) 按钮设置为在单击时执行任何操作。 我有 3 个活动(MainActivity、Second、Third)并且我已经在所有 3 个中定义了工具栏但是当我在每个活动中单击主页按钮时没有任何反应我希望按钮执行他的操作(在我的理解)返回首页的默认操作 Activity 即 "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>
当然还有我的 java 代码的一部分,用于我的 3 个活动中的每一个: 主要Activity:
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);
}
});
第二Activity:
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);
}
});
}
和第三Activity:
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 个 Activity "onCreate" 功能几乎相同..
当我启动我的应用程序时,我可以在所有 3 个活动中看到工具栏,我可以在所有活动中看到后退箭头,但单击它什么都不做。
我知道我可以设置 toolbar.setNavigationOnClickListener
但我只是想让它成为默认操作,即回家 activity...
非常感谢你,
诺姆
试试这个:
@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;
}
这样试试...
@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);
}
如果您有 parent activity,请遵循此代码,
@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);
}
在你的活动中试试这个
@Override
public boolean onOptionsItemIdSelected(int id) {
switch (id) {
case R.id.home:
//do stuff
break;
}
return true;
}
对于您的情况,我建议您创建一个继承所有活动的 AbstractActivity。所以你可以考虑一些行为,包括工具栏按钮的管理