
Untitled
By: a guest on
May 9th, 2012 | syntax:
None | size: 1.34 KB | hits: 32 | expires: Never
Button Click Event on Android
<Button
android:id="@+id/btnOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Message"
android:onClick="onBtnClicked" />
package com.example.helloandroid;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class HelloAndroid extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onBtnClicked(View v)
{
if(v.getId() == R.id.btnOK)
{
MessageBox("Hello World");
}
}
public void MessageBox(String message)
{
Toast.makeText(this, message, Toast.LENGTH_SHORT);
}
}
Toast.makeText(this, message, Toast.LENGTH_SHORT);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
Button b = (Button) findViewById(R.id.btnOK);
b.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.btnOK:
/* the instruccions of the button */
break;
}
}