|
|||||
ボタンの長押しイベントボタンと言えば、View.OnClickListenerが良く使われるかと思うが、長押しに対応したView.OnLongClickListerも用意されている。ボタン長押し時に発生するonLongClickイベントの戻り値でtrueを返すとイベントは消費され、通常のonClickイベントは発生しない。
package biz.office_matsunaga;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class LongClickTestActivity extends Activity {
/** Called when the activity is first created. */
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "長押し", Toast.LENGTH_SHORT).show();
return true; // 戻り値をtrueにするとOnClickイベントは発生しない
}
});
}
}
(2012/01/24)
Copyright(C) 2004-2013 モバイル開発系(K) All rights reserved.
[Home]
|
|||||