Reputation: 11
package com.example.sudoku;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Sudoku extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up click listeners for all the buttons
View continueButton=findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton=findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton=findViewById(R.id.about_button);
aboutButton.setOnClickListener(Sudoku.this);
View exitButton=findViewById(R.id.exit_button);
exitButton.setOnClickListener(Sudoku.this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.about_button:
Intent i=new Intent(Sudoku.this, About.class);
startActivity(i);
break;
case R.id.exit_button:
finish();
break;
}
}
}
when i click views-buttons nothing happens i know that views-buttons are not connected with the on click method at all because when i define on click method into the x m l file it works fine,any help please i am new to android programming,thnx
Upvotes: 1
Views: 173
Reputation: 110
You might find it easier to pass add the onClick attribute to your xml. In the xml element you want to handle a click for, add this line:
android:onClick = "AddViewX"
Then, in your activity class you want to create a method like this:
public void AddViewX(View v){
//Write Your Code Here
Intent i=new Intent(this, NextClass.class);
startActivity(i);
}
This way you don't have to set up OnClickListeners, which can get very confusing, very fast.
Upvotes: 0
Reputation: 6277
Replace View exitButton=findViewById(R.id.exit_button);
with
View exitButton=(View)findViewById(R.id.exit_button);
Upvotes: 0
Reputation: 1321
write the cases for remaining buttons also,
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.about_button:
Intent i=new Intent(Sudoku.this, About.class);
startActivity(i);
break;
case R.id.exit_button:
finish();
break;
case R.id.continueButton:
//your function
break;
case R.id.newButton:
//your function
break;
}
}
Upvotes: 0
Reputation: 11131
change onClick() method like this
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.about_button:
Intent i=new Intent(Sudoku.this, About.class);
startActivity(i);
break;
case R.id.exit_button:
finish();
break;
case R.id.new_button:
newButtonClicked(); // do your work here
break;
case R.id.continue_button:
continueButtonClicked(); // do work
break;
}
}
Upvotes: 1
Reputation: 1746
You didn't define listener for other 2 buttons that is new & continue. Click on your exit button and see how it responds. Does it finish the application? If so you are on the right track.
Upvotes: 1