Registering Events for Views :
"Views can fire events when users interact with them. For example, when a user touches a Buttonview, you need to service the event so that the appropriate action can be performed. To do so, youneed to explicitly register events for views.Using the same example discussed in the previous section, recall that the activity has two Buttonviews; therefore, you can register the button click events using an anonymous class as shown here:"
exj:
package net.learn2develop.UIActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(btnListener);
Button btn2 = (Button)findViewById(R.id.btn2);
btn2.setOnClickListener(btnListener);
}
// create an anonymous class to act as a button click listener
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getBaseContext(),
((Button) v).getText() + “ was clicked”,
Toast.LENGTH_LONG).show();
}
};
override the keydown method
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
//...
//...
}
return false;
}
}
"If you now press either the OK button or the Cancel button, the appropriate message will be displayed
"Views can fire events when users interact with them. For example, when a user touches a Buttonview, you need to service the event so that the appropriate action can be performed. To do so, youneed to explicitly register events for views.Using the same example discussed in the previous section, recall that the activity has two Buttonviews; therefore, you can register the button click events using an anonymous class as shown here:"
exj:
package net.learn2develop.UIActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(btnListener);
Button btn2 = (Button)findViewById(R.id.btn2);
btn2.setOnClickListener(btnListener);
}
// create an anonymous class to act as a button click listener
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getBaseContext(),
((Button) v).getText() + “ was clicked”,
Toast.LENGTH_LONG).show();
}
};
override the keydown method
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
//...
//...
}
return false;
}
}
"If you now press either the OK button or the Cancel button, the appropriate message will be displayed
proving that the event is wired up properly."
No comments:
Post a Comment