如何在操作栏上添加后退箭头或后退按钮

时间:2015-03-30 01:15:18

标签: android android-activity android-actionbar

如何在Android App操作栏的左上角放置后退箭头或后退按钮。下面是代码和截图。

我经历了很多教程,每次遇到某种错误。所以现在我在这里发布了我的代码,我希望有人肯定会帮助我。谢谢:))

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTitle("Search Station");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);   
    editIntent = new Intent(this, EditStationActivity.class);
}

public void onResume(){
    super.onResume();
    updateList("");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.add_station){
        Intent intent = new Intent(this, AddStationActivity.class);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

public void findStation(View view) {
    EditText keywrd = (EditText) findViewById(R.id.keyword);
    String keyword = keywrd.getText().toString();
    updateList(keyword);
}

public void updateList(String condition) {
    DbHandler dbh = new DbHandler(this);
    SQLiteDatabase db = dbh.getReadableDatabase();
    String[] projection = {
            DbHandler.StationsHandler.COLUMN_NAME_ENTRY_ID,
            DbHandler.StationsHandler.COLUMN_NAME_TITLE
    };

    Cursor c;
    if (!condition.equals("")) {
        String cond = DbHandler.StationsHandler.COLUMN_NAME_TITLE+" LIKE ?";
        String[] params = {
            "%"+condition+"%"
        };

        c = db.query(DbHandler.StationsHandler.Table_Name, projection, cond, params, null, null, null);
    } else
        c = db.query(DbHandler.StationsHandler.Table_Name, projection, null, null, null, null, null);

    if(c.getCount() == 0) {
        items = new String[1];
        items[0] = "No Station found";

        ids = new String[1];
        ids[0] = "0";
    } else {
        items = new String[c.getCount()];
        ids = new String[c.getCount()];

        c.moveToFirst();

        int i = 0;
        do {
            ids[i] = c.getString(0);
            items[i++] = c.getString(1);
        }
        while (c.moveToNext());
    }

    db.close();
    dbh.close();

    ListView stationList = (ListView) findViewById(R.id.station_list);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    stationList.setAdapter(adapter);

    stationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(ids[position].equals("0"))
                return;

            editIntent.putExtra(EDITID, ids[position]);
            startActivity(editIntent);
        }
    });
}

2 个答案:

答案 0 :(得分:3)

将您的活动扩展为ActionBarActivity,并在onCreate()方法中使用以下内容  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

将此添加到您的活动中

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) { 
case android.R.id.home: 
onBackPressed();
 return true;
 default:
 return super.onOptionsItemSelected(item); 
} 
}

答案 1 :(得分:2)

只需在AndroidManifest.xml文件中指定活动的父级。

 <activity
        android:name=".EditStationActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:launchMode="singleTop"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
</activity>