アプリ開発初心者の暇つぶしAndroid体験記

アプリ開発初心者がAndroidアプリ開発始めました。日々学んだことをアウトプットしていきます。

【Android】OnLongClickListenerでボタンの長押し処理を実装


今回はボタンを長押しした際の処理の実装についてまとめました。
それではどうぞ。

設定するイベントリスナー

ボタン長押しというイベントを検知させるためには、
OnLongCickListenerを実装します。

findViewById(R.id.button).setOnLongClickListener(this);

長押しを検知した時の処理

次はボタンが押された時の処理を見ていきます。
onLongClickメソッドをオーバーライドして処理を実装します。

@Override
public boolean onLongClick(View v) {
    text2.setText("onLongClickメソッドが呼ばれました");
    return true;
}

メソッドの戻り値の型がbooleanなのが注目ですね!

true、falseでそれぞれ以下のように動きが変わります。

  • trueの場合

onClickメソッドは実行されない。

  • falseの場合

onClickメソッドも続けて実行される。
(もちろん実装してあれば)

実際の動きを確認してみる

サンプルのアプリケーションを作ったので、動かしてみます。

[avtivity_main.xml]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="clear" />

</LinearLayout>


[MainActivity.java]

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

    TextView text;
    TextView text2;

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

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

        findViewById(R.id.button2).setOnClickListener(this);

        text = findViewById(R.id.textView);
        text2 = findViewById(R.id.textView2);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:

                text.setText("onClickメソッドが呼ばれました");
                break;

            case R.id.button2:

                text.setText("");
                text2.setText("");
                break;
        }
    }

    @Override
    public boolean onLongClick(View v) {
        text2.setText("onLongClickメソッドが呼ばれました");
        return true;
    }
}

設置したボタンを長押しした時の動きがこのようになります。
f:id:mtnanao:20200305233619p:plain

onLongClickメソッドがtrueを返していますので、
onLongClickメソッドのみが呼ばれているのが分かると思います。

それでは、falseを返すようにして再度実行してみます。
f:id:mtnanao:20200305233836p:plain

onLongClickメソッドの後に、onClickメソッドが実行されています。


それでは、今回は以上です(^_^)/
お疲れさまでした!!


最後にアプリの宣伝させてください(*^-^*)

play.google.com