如何在android 的actionbar中添加返回按钮
2个回答
2016-05-13 · 知道合伙人教育行家
关注
展开全部
1.分别创建MainActivity 和DetailActivity,并作如下配置
package="com.example.google"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light" >
android:name="com.example.google.MainActivity"
android:label="@string/app_name" >
android:name=".DetailActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.google.MainActivity" >
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.google.MainActivity" />
2.详情DetailActivity主要做如下配置:
... >
...
android:name="com.example.myfirstapp.MainActivity" ...>
...
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
以及:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}3.源码如下
1>MainActivity
package com.example.google;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements
OnQueryTextListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);
}
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
// 抄袭ApiDemo 让搜索输入框 内嵌到 actionbar中
if (android.os.Build.VERSION.SDK_INT >= 11) {
SearchView searchView = (SearchView) menu.findItem(
R.id.action_search).getActionView();
// 实现搜索监听
searchView.setOnQueryTextListener(this);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "搜索", 0).show();
return true;
case R.id.action_settings:
Toast.makeText(this, "设置", 0).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// 搜索提交
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(this, query, 0).show();
return true;
}
// 搜索文本发生改变
@Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(this, newText, 0).show();
return true;
}
}
2>DetailActivity
package com.example.google;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class DetailActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
3>res/menu/main.xml
xmlns:test="http://schemas.android.com/apk/res-auto" >
android:id="@+id/action_search"
test:actionViewClass="android.support.v7.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
test:showAsAction="ifRoom"/>
android:id="@+id/action_settings"
android:title="@string/action_settings"
test:showAsAction="never"/>
package="com.example.google"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light" >
android:name="com.example.google.MainActivity"
android:label="@string/app_name" >
android:name=".DetailActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.google.MainActivity" >
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.google.MainActivity" />
2.详情DetailActivity主要做如下配置:
... >
...
android:name="com.example.myfirstapp.MainActivity" ...>
...
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
以及:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}3.源码如下
1>MainActivity
package com.example.google;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements
OnQueryTextListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);
}
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
// 抄袭ApiDemo 让搜索输入框 内嵌到 actionbar中
if (android.os.Build.VERSION.SDK_INT >= 11) {
SearchView searchView = (SearchView) menu.findItem(
R.id.action_search).getActionView();
// 实现搜索监听
searchView.setOnQueryTextListener(this);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "搜索", 0).show();
return true;
case R.id.action_settings:
Toast.makeText(this, "设置", 0).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// 搜索提交
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(this, query, 0).show();
return true;
}
// 搜索文本发生改变
@Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(this, newText, 0).show();
return true;
}
}
2>DetailActivity
package com.example.google;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class DetailActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
3>res/menu/main.xml
xmlns:test="http://schemas.android.com/apk/res-auto" >
android:id="@+id/action_search"
test:actionViewClass="android.support.v7.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
test:showAsAction="ifRoom"/>
android:id="@+id/action_settings"
android:title="@string/action_settings"
test:showAsAction="never"/>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询