Android 当onClick已经在XML中时,单击长实现?
事实上,我已经阅读了一些答案,但它们与我实现单击响应的简单方式大不相同,我想知道是否有一种方法可以在我正在做的事情中添加一些简单的内容,以创建一个onLongClick响应 基本上,我所有的XML代码都是用如下语句编写的:Android 当onClick已经在XML中时,单击长实现?,android,android-layout,Android,Android Layout,事实上,我已经阅读了一些答案,但它们与我实现单击响应的简单方式大不相同,我想知道是否有一种方法可以在我正在做的事情中添加一些简单的内容,以创建一个onLongClick响应 基本上,我所有的XML代码都是用如下语句编写的: android:onClick="onSync" 那么我的Java有: public void onSync(View v) { ... Toast toast3=Toast.makeText(this, "Sync was
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"/>
这不能通过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;
}
});
Button-Button=(Button)findViewById(R.id.);
setOnLongClickListener(新的OnLongClickListener(){
@凌驾
仅长按公共布尔值(视图v){
//TODO自动生成的方法存根
返回true;
}
});
确保此代码在调用setContentView()
之后出现
另外,确保android:longClickable
属性设置为true
在XML中,ID设置为
buttonSync
,而在Java代码中使用的是button\u sync
。这就是产生NullPointerException
的原因,因为您没有名为button\u sync
的按钮,因此无法通过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;
}
});
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;
}
};
}
Button-Button=(Button)findViewById(R.id.);
setOnLongClickListener(新的OnLongClickListener(){
@凌驾
仅长按公共布尔值(视图v){
//TODO自动生成的方法存根
返回true;
}
});
确保此代码在调用setContentView()
之后出现
另外,确保android:longClickable
属性设置为true
在XML中,ID设置为
buttonSync
,而在Java代码中使用的是button\u sync
。这就是您出现NullPointerException
的原因,因为您没有一个名为button\u sync
的按钮,可能我需要多一点上下文,因为我得到的只是错误。我进入正确,但我不确定该代码相对于我发布的代码去了哪里。Eclipse最后做了一些奇怪的事情,删除了我最后的5个版本,所以我不能发布任何代码。。。现在正在重新创建我的代码。让我补充一句,这可能是很棒的代码,但不知道放在哪里,如果它是完整的,这是非常令人沮丧的。在Stockoverflow中有许多长按的代码示例,但它们都会受到这种情况的影响,imho。Android官方文档非常完整,要么没有运行,要么没有示例。此代码与其他任何.setXXX()方法类似。第一行可以在setContentView()之后的任何时间调用,第二部分可以在它之后的任何时间(通常是立即)调用。就我个人而言,我通常把它放在onCreate()
中。谢谢,这很有帮助。我的代码没有setContentView(),所以我想我需要对此进行研究。我确实在onCreate()中尝试了代码,但可能是缺少setContentView()导致了错误。您是如何绘制布局的?如果它来自XML文件,那么必须在某个地方有一个setContentView()。如果它是动态创建的,那么只需使用与创建按钮相同的按钮对象即可。但既然您在代码中提到了XML,我的回答是假设的。也许我需要多一点上下文,因为我得到的只是错误。我进入正确,但我不确定该代码相对于我发布的代码去了哪里。Eclipse最后做了一些奇怪的事情,删除了我最后的5个版本,所以我不能发布任何代码。。。现在正在重新创建我的代码。让我补充一句,这可能是很棒的代码,但不知道放在哪里,如果它是完整的,这是非常令人沮丧的。在Stockoverflow中有许多长按的代码示例,但它们都会受到这种情况的影响,imho。Android官方文档非常完整,要么没有运行,要么没有示例。此代码与其他任何.setXXX()方法类似。第一行可以在setContentView()之后的任何时间调用,第二部分可以在它之后的任何时间(通常是立即)调用。就我个人而言,我通常把它放在onCreate()
中。谢谢,这很有帮助。我的代码没有setContentView(),所以我想我需要对此进行研究。我确实在onCreate()中尝试了代码,但可能是缺少setContentView()导致了错误。您是如何绘制布局的?如果它来自XML文件,那么必须在某个地方有一个setContentView()。如果它是动态创建的,那么只需使用与创建按钮相同的按钮对象即可。但既然您在代码中提到了XML,我的回答是假设的。
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;
}
};
}