onCreate() {..View silentButton = findViewById(R.id.silent_button);silentButton.setOnClickListener(this);....}public void onClick(View view) {switch (view.getId()) {case R.id.silent_button:mAudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);break;case R.id.normal_button:mAudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);break;}And this is how one should codeHowever, instead of applying an
OnClickListenerto the button in your activity, you can assign a method to your button in the XML layout, using theandroid:onClickattribute. For example:Now, when a user clicks the button, the Android system calls the activity's
selfDestruct(View)method. In order for this to work, the method must be public and accept aViewas its only parameter. For example:public void selfDestruct(View view) {
// Kabloey
}The
Viewpassed into the method is a reference to the widget that was clicked.I knew this because I have started working on Catroid. Thanks
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
0 comments:
Post a Comment