Ich weiß, das ist eine Frage, die häufig gestellt wird, aber nach dem Lesen der vielen Fragen und Lösungen auf Stack Overflow bin ich verwirrt. Ich bin verwirrt in Bezug auf Fragments
und was benötigt wird, um eine Aktivität durch Klicken auf ein Element in der Navigationsleiste zu starten.Starten einer neuen Aktivität auf Navigationselement klicken
Ich habe diese Beiträge überprüft werden aber nur Q1, verwirrte Q2
Könnte jemand bitte erklären, was erforderlich ist, eine Grundaktivität von diesem Navigationsleiste Elemente gestartet werden? Benötige ich eine onClick
Methode implementiert wo im Code angegeben? Wie verhält es sich mit der Absicht?
Hier mein MainActivity.java
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
NavigationView navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initInstances();
}
private void initInstances() {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.hello_world, R.string.hello_world);
drawerLayout.setDrawerListener(drawerToggle);
navigation = (NavigationView) findViewById(R.id.navigation_view);
navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.navigation_item_1:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.navigation_item_2:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.navigation_item_3:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.navigation_item_4:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.navigation_item_5:
//Do some thing here
// add navigation drawer item onclick method here
break;
}
return false;
}
});
}
@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation_view_items, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item))
return true;
// 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();
//noinspection SimplifiableIfStatement
if (id == R.string.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
ist Und hier ist die zweite Aktivität, Playboard.java, lädt das einfach ein Hintergrundbild:
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Playboard extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playboard);
}
}
Alle Eingaben sehr geschätzt Dank!
verwenden Vorsatz muss ich ausdrücklich jede Bibliothek importieren? Ich habe das gerade jetzt versucht und habe einen Fehler bei der Absicht und ich – choloboy
Es gibt eine erforderliche Import-Anweisung in der Datei: 'import android.content.Intent;'. Android Studio und Eclipse können jedoch helfen, fehlende Importe für Sie zu beheben. –
Arbeitete wie ein Charme. Vielen Dank. – choloboy