Thursday, February 13, 2014

Rookie help! onClick (DialogInterface dialog, int which) {} and onClick (View v) {} different


            
I rewrite onclicklistener method when eclipse gave me automatically generated onClick (DialogInterface dialog, int which) {} method, but I used setOnClickListener () error when running the simulator, I used to watch video on a onClick (View v) {} method, but I do not have this method of sdk1.6 ah! Which prawn pointing to it!
Attached code:
public class Activity01 extends Activity {
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
TextView myTextView = (TextView) findViewById (R.id.myTextView);
Button myButton = (Button) findViewById (R.id.myButton);
myTextView.setText ("My first TextView");
myButton.setText ("My First Button");
myButton.setOnClickListener ((android.view.View.OnClickListener) new myButtonListener ());
}
class myButtonListener implements OnClickListener {

public void onClick (DialogInterface dialog, int which) {
/ / TODO Auto-generated method stub
Intent intent = new Intent ();
intent.setClass (Activity01.this, Activity02.class);
}

}
}
Reply:
button's onClick event: button = (Button) findViewById (R.id.button);
button.setOnClickListener (new OnClickListener () {
public void onClick (View view) {

}
});
Importing package for: import android.view.View.OnClickListener;
Do not mistake the whole
Reply:
Well, ah, I would also add that Activity01.this.startActivity (intent) below; then be able to run, thanks
Reply:
Well, indeed guide the wrong package, and encountered the same problem, thank answers
Reply:
Solve the problem ⋯ ⋯
Reply:
Do not know ah
Reply:
Today I also guide the wrong package Haha, resolved. . Thank you heroes
Reply:
Thank you for help, and finally know why the wrong
Reply:
Personally think the program should be like this:
public class Activity01 extends Activity {
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
TextView myTextView = (TextView) findViewById (R.id.myTextView);
Button myButton = (Button) findViewById (R.id.myButton);
myTextView.setText ("My first TextView");
myButton.setText ("My First Button");
myButton.setOnClickListener ( new myButtonListener ());
}
class myButtonListener implements android.view.View.OnClickListener {

public void onClick (View v) {
...
Reply:
Why do I import android.view.View.OnClickListener this package, the following is the red line, this would not have changed android.view.View, but have to let me create android.view.View.OnClickListener, Why Why ah ah
Reply:
I tried again, this would be on the program, but running a virtual machine, or error, angry ah. Two days, or not so
Reply:
I was such a problem, ah, the same requirements. Depressed dead are,
Reply:
After a run, 8th floor program is correct
Reply:
So the problem is the landlord of different packages and mars the package is right
Reply:
And it was so, collection, thank you! !
cited a floor javasoldiers reply:
button's onClick event: button = (Button) findViewById (R.id.button);
button.setOnClickListener (new OnClickListener () {
public void onClick (View view) {

}
});
Importing package for: import android.view.View.OnClickListen ......

Reply:
Learning is the problem
Reply:
cited a floor reply:
button's onClick event: button = (Button) findViewById (R.id.button);
button.setOnClickListener (new OnClickListener () {
public void onClick (View view) {

}
});
Importing package for: import android.view.View.OnClickListen ......


I have encountered this problem, but this changed according to your future, when running, is this Button is an error. . Seeking large cattle facie code, where there wrong! ! Hurry! ! Thank you, thank
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity3 extends Activity {
private TextView symbol;
private EditText num1;
private EditText num2;
private Button calculate;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity3);
num1 = (EditText) findViewById (R.id.num1);
num2 = (EditText) findViewById (R.id.num2);
symbol = (TextView) findViewById (R.id.symbol);
calculate = (Button) findViewById (R.id.calculate);
symbol.setText ("multiply");
calculate.setText ("computing");
/ / Bind the listener to a button object above
/ / Calculate.setOnClickListener (new CalculateListener ());
calculate.setOnClickListener (new OnClickListener ()
{
public void onClick (View view) {
/ / TODO Auto-generated method stub
/ / Get the value entered by the user in
EditTextString num1Str = num1.getText () toString ();.
String num2Str = num2.getText () toString ();.
/ / These two values ​​into the intent object
Intent intent = new Intent ();
intent.putExtra ("num1", num1Str);
intent.putExtra ("num2", num2Str);
intent.setClass (Activity3.this, ResultActivity.class);
/ / Use this intent to initiate Result
Activity3.this.startActivity (intent);

}
});
}
Reply:
But I took the above steps changed, when running on the emulator point Button or error ah, seeking advice cattle! ! Urgent! Thank you!
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity3 extends Activity {
private TextView symbol;
private EditText num1;
private EditText num2;
private Button calculate;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity3);
num1 = (EditText) findViewById (R.id.num1);
num2 = (EditText) findViewById (R.id.num2);
symbol = (TextView) findViewById (R.id.symbol);
calculate = (Button) findViewById (R.id.calculate);
symbol.setText ("multiply");
calculate.setText ("computing");
/ / Bind the listener to a button object above
/ / Calculate.setOnClickListener (new CalculateListener ());
calculate.setOnClickListener (new OnClickListener ()
{
public void onClick (View view) {
/ / TODO Auto-generated method stub
/ / Get the value entered by the user in
EditTextString num1Str = num1.getText () toString ();.
String num2Str = num2.getText () toString ();.
/ / These two values ​​into the intent object
Intent intent = new Intent ();
intent.putExtra ("num1", num1Str);
intent.putExtra ("num2", num2Str);
intent.setClass (Activity3.this, ResultActivity.class);
/ / Use this intent to initiate Result
Activity3.this.startActivity (intent);

}
});
}
Reply:
referenced 8th Floor reply:
personally think the program should be like this:
public class Activity01 extends Activity {
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (save ......

Everyone's problem and it would be wise to resolve this feeling too cool, we all come on!
Reply:
# 8 powerful, positive solution, I suddenly tuned, thanks
Reply:
Thank 8th floor. Because there are people like you, only to make the world a better place.

No comments:

Post a Comment