HOME技術AndroidAndroidアプリ:クリックイベントとロングクリックイベントを取得する

Androidアプリ:クリックイベントとロングクリックイベントを取得する

Androidアプリで、クリックイベントとロングクリックイベントの2つを取得して処理する方法です。

OnClickListenerとOnLongClickListenerを併記

public class MainActivity extends Activity implements View.OnClickListener,View.OnLongClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
button.setOnLongClickListener(this);

}

@Override
public void onClick(View v){
  Toast.makeText(getApplicationContext(),"タップ",Toast.LENGTH_SHORT).show();
}

@Override
public boolean onLongClick(View v){
  Toast.makeText(getApplicationContext(),"長押しタップ",Toast.LENGTH_SHORT).show();
  return true;
}
}

レイアウトxml上にidがbuttonというボタンを配置します。
タップしたときは、「タップ」と表示され、ロングタップしたときは「長押しタップ」と表示されます。

関連記事

Androidアプリ:App is not indexable by Google Search;と表示される

Android Studioで、 App is not indexable by Google Search; consider adding at least one Activity with a…続きを読む

Google広告でポリシー違反「句読点と記号」に対応する

モバイルアプリのGoogle広告のキャンペーンを設定しましたが、「ポリシー違反1件」と表示されました。 ポリシー違反「句読点と記号」 ポリシー違反の詳細を見ると、「句読点と記号」というヒントが表示され…続きを読む

Androidアプリ:ストアの掲載情報

Androidアプリのプログラムが完成したらすぐにGoogle Playにアップロードできるわけではありません。 プログラム以外に必要なデータを以下にリストアップしました。 ストアの掲載情報として必要…続きを読む

Androidアプリ:When using intent filters, please specify ‘android:exported’ as well

Android Studioで「When using intent filters, please specify ‘android:exported’ as well」という…続きを読む

Androidアプリ:テキストをリスト表示する

Androidアプリで、ListViewを使い、配列に格納したテキストをリスト表示します。 MainActivity.java import android.app.Activity; import …続きを読む

Androidアプリ:ボタンの表示/非表示

Androidでボタンの表示/非表示をプログラム上からコントロールします。 MainActivity.java   Button button = findViewById(R.id.but…続きを読む

Androidアプリ:特定のViewに最初のフォーカスをあてる

Android Studioにおいて、起動時に特定のViewにフォーカスをあてる方法です。 activity_main.xmlで設定 レイアウト定義ファイル(activity_main.xml)におい…続きを読む

Androidアプリ:Firebaseを利用したAdMobの実装方法

AndroidアプリでFirebaseを利用したAdMobの実装方法を調べました。 なお、FirebaseおよびAdMobには既にユーザ登録済みであることを想定しています。 未登録の場合は先に登録して…続きを読む

Androidアプリ:error: expected color reference but got (raw string)

Android Studioでビルドした際に、C:\android\project\***\app\src\main\res\values\styles.xml: AAPT: error: expec…続きを読む

Androidアプリ:ボタンを押すとメッセージが入れ替わる

Android StudioでAndroidアプリを試作します。 今回は「ボタンを1回押すとテキストの内容が変わり、もう1回押すと元に戻る」というだけのアプリをつくります。 プロジェクトの生成 And…続きを読む

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny