0

私は実際にこれに対するいくつかの回答を読みましたが、クリック応答を実装している単純な方法とは大きく異なるため、onLongClick 応答を作成するために行っていることに単純なものを追加する方法があるかどうか疑問に思っています。

基本的に、すべての XML コードは次のようなステートメントで記述されています。

android:onClick="onSync"

次に、私のJavaには次のものがあります。

    public void onSync(View v) {
            ...
        Toast toast3=Toast.makeText(this, "Sync was pressed",Toast.LENGTH_SHORT);
        toast3.show();
    }

私がやりたいのは、ボタンが長押しされたときに呼び出される別の機能を持つことです。現在、長押しは短押しと同じアクションを引き起こします。

具体的には、次のようなルーチンにインターフェイスする方法を知りたいです。

        public void onSyncLong(View v) {
            ...
        Toast toast3=Toast.makeText(this, "Long Sync was pressed",Toast.LENGTH_SHORT);
        toast3.show();
    }

この問題について何か助けていただければ幸いです。XML と Jave で何をすべきかを返信で教えてくれたら最高です。本当にありがとう。

- - - - - - - - - - - - - - アップデート - - - - - - - - - - - ---

これが私のonCreateコードです:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.start_meters);

    textLL = (TextView)findViewById(R.id.textLL);
    textTimer = (TextView)findViewById(R.id.textTimer);
    textTimeToLine = (TextView)findViewById(R.id.textTimeToLine);


    Button button = (Button) findViewById(R.id.button_sync);

    button.setOnLongClickListener(new OnLongClickListener() { 
            @Override
            public boolean onLongClick(View v) {

                // TODO Auto-generated method stub
                return true;
            }
        });

}

ボタンの XML セグメントは次のとおりです。

        <Button
    android:id="@+id/buttonSync"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Gun/Sync" 
    android:onClick="onSync"
    android:textSize="@dimen/font_small"
    android:background="@drawable/round_button"
    android:padding="3sp"
    android:longClickable="true"/>

------------最終更新----------------

作業コードは次のとおりです。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.start_meters);

    textLL = (TextView)findViewById(R.id.textLL);
    textTimer = (TextView)findViewById(R.id.textTimer);
    textTimeToLine = (TextView)findViewById(R.id.textTimeToLine);


    Button button = (Button) findViewById(R.id.buttonSync);

    button.setOnLongClickListener(new OnLongClickListener() { 
            @Override
            public boolean onLongClick(View v) {

                StartLine2.startTime = pTime + 1000*60*5;
                return true;
            }
        });

}
4

2 に答える 2

4

XML 経由でこれを行うことはできません。代わりに、次を使用します。

Button button = (Button) findViewById(R.id.<your_id>);

button.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return true;
        }
    });

このコードsetContentView()が呼び出された後に来ることを確認してください。

また、android:longClickableプロパティが true に設定されていることを確認してください。

XML では、ID は に設定されてbuttonSyncいますが、使用している Java コードではbutton_sync. NullPointerExceptionというボタンがないため、これが の理由ですbutton_sync

于 2012-11-22T16:55:13.850 に答える
0
public class GameScoreFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        Log.v("TTT", "GameScoreFragment.OnCreateView()");

        View viewScore = inflater.inflate(R.layout.gamescorelayout, container, false);


        // set onLongClick listeners for both buttons
        // when player long presses any of the two buttons, scores are reset

        Button tempButton = (Button) viewScore.findViewById(R.id.id_button_pone_score);
        tempButton.setOnLongClickListener( mLongListener );

        tempButton = (Button) viewScore.findViewById(R.id.id_button_ptwo_score);
        tempButton.setOnLongClickListener( mLongListener );

        return viewScore;
    }


    // define a variable mLongListener to hold the listener code
    // and then use mLongListener to set the listener
    // if we don't define the variable, then we will have to write the listener code at two places (once for each button)

    private View.OnLongClickListener mLongListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View pView) {
            //reset player scores
            GameFragment tempGameFragment = (GameFragment) getFragmentManager().findFragmentById(R.id.id_gamefragment);
            if (tempGameFragment != null)
                tempGameFragment.resetPlayersScore(false);

            return true;
        }
    };
}
于 2015-12-19T07:21:19.607 に答える