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 回答