Switch case in Android-app
Hier ist der code, soll ein Dialogfeld zu öffnen, wenn ich drücken Sie die Schaltfläche info in meiner min-Aktivität Seite. Aber nichts wirklich passiert. : /
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newgameButton = findViewById(R.id.new_button);
continueButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
continueButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
continueButton.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.continue_button:
break;
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.new_button:
openNewGameDialog();
break;
case R.id.exit_button:
finish();
break;
}
}
Haupttätigkeit XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="30dip"
android:orientation="horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center" >
<TextView
android:text="@string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label" />
<Button
android:id="@+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_label" />
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_label" />
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_label" />
</LinearLayout>
</LinearLayout>
Beispiel für eine Schaltfläche: Über
Über Die Tätigkeit
public class About extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.about, menu);
return true;
}
}
Über XML -
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text" />
</ScrollView>
post gesamte Tätigkeit code
bitte fügen Sie bestimmten code Ihrer Tätigkeit . so dass ich Ihnen helfen kann
ok, hier ist der code.
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R. layout.main); Ansicht continueButton = findViewById(R. id.continue_button); continueButton.setOnClickListener(this); View newgameButton = findViewById(R. id.new_button); continueButton.setOnClickListener(this); View aboutButton = findViewById(R. id.about_button); continueButton.setOnClickListener(this); exitButton = findViewById(R. id.exit_button); continueButton.setOnClickListener(this); }
Sorry für das Durcheinander . Ich bin ziemlich neu in diesem
bitte fügen Sie bestimmten code Ihrer Tätigkeit . so dass ich Ihnen helfen kann
ok, hier ist der code.
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R. layout.main); Ansicht continueButton = findViewById(R. id.continue_button); continueButton.setOnClickListener(this); View newgameButton = findViewById(R. id.new_button); continueButton.setOnClickListener(this); View aboutButton = findViewById(R. id.about_button); continueButton.setOnClickListener(this); exitButton = findViewById(R. id.exit_button); continueButton.setOnClickListener(this); }
Sorry für das Durcheinander . Ich bin ziemlich neu in diesem
InformationsquelleAutor SagiAharon | 2013-11-13
Du musst angemeldet sein, um einen Kommentar abzugeben.
Hier ist dein problem:
Nur Ihre 'continueButton' hat einen onClickListener registriert...
Auch:
Es ist eine gute Praxis, haben auch einen default-Fall in einer switch-Anweisung, die ausgeführt wird, wenn keiner der Fälle halten. Fügen Sie ein Standard-Fall mit einer log-Meldung, oder etwas und sehen, wenn es ausgeführt wird.
v.GetId()
ist, wielog.d("someActivity", someConvertToStringMethod( v.GetId() ) )
.ok danke ich werde es versuchen
Toll. Es bekam. In Bezug auf das Log.id(KONSTANTE, msg) - scheint, ich kann den Griff bekommen. Wenn ich will, zu bekommen, können sagen, dass die Ausgabe für die i-Taste gedrückt, können sagen, ich drückte den continueButton, was soll ich schreiben in der Log.d? (Ich meine statt "msg"). Und auch, wo sehe ich das Ergebnis? (ich weiß, es ist in der logcat, aber es gibt so viele ausgegeben... )
InformationsquelleAutor Harry
Erstens, stellen Sie sicher, dass Ihre Aktivität implementiert Ansicht.OnClickListener Schnittstelle.
Zweitens, versuchen Sie, die add @Override vor die public void onClick(View View) { } Methodendeklaration. Wenn dies nicht hilft, versuchen Sie, fügen Sie eine log-Anweisung am Anfang der onClick-Methode, zum Beispiel die Protokollierung, die ID der view und vergleichen Sie es mit denen, die Sie versuchen zu entsprechen gegen.
InformationsquelleAutor damian
Soweit ich das sagen kann, müssen Sie rufen Sie
button.setOnClickListener(this);
in der activity onCreate-MethodeInformationsquelleAutor NARESH REDDY