btn.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(
InputEx.this,
"click",
Toast.LENGTH_LONG)
.show();
}
}
);
另一種
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
//Declaration Button
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Intialization Button
btn = (Button) findViewById(R.id.btnClickMe);
btn.setOnClickListener(MainActivity.this);
//Here MainActivity.this is a Current Class Reference (context)
}
@Override
public void onClick(View v) {
//Your Logic
}
}
全站熱搜
留言列表

