如何知道点击了什么按钮?
How to know what button clicked?
我创建了两个片段:
<fragment
android:name="BoxFragment"
android:layout_width="150dp"
android:layout_height="80dp"
android:id="@+id/box_fragment_1"
android:layout_below="@+id/box_fragment_2"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp"
tools:layout="@layout/fragment_box" />
<fragment
android:layout_width="150dp"
android:layout_height="50dp"
android:id="@+id/box_fragment_2"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
tools:layout="@layout/fragment_box" />
此片段的布局:
<ImageButton
android:layout_width="197dp"
android:layout_height="197dp"
android:id="@+id/ibPress"
android:layout_gravity="center" />
所以,我注册了 OnClickListener:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_box, container, false);
ImageButton imageButton= (ImageButton) view.findViewById(R.id.ibPress);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(LOG_TAG,this.toString());
//how to know,wich button clicked?
presenter.onButtonClick(...?);
}
});
return view;
}
所以,我想知道按下了哪个片段按钮?
我是否需要将 View 引用到 presenter.onButtonClick
?
在演示者,我想知道按下了什么按钮,然后做一些工作。
谢谢!
您可以使用getId()
获取当前点击视图的id
。参见下面的代码。
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.b1:
// it was the first button
break;
case R.id.b2:
// it was the second button
break;
}
}
}
请阅读 docs 了解更多信息。
您可以通过以下方式修改您的 presenter.onButtonClick()
presenter.onButtonClick(int fragmentId)
或 presenter.onButtonClick(String fragmentTagName)
然后在使用 switch case
的方法中,您可以根据该特定片段的 ID 或标签执行操作。
我创建了两个片段:
<fragment
android:name="BoxFragment"
android:layout_width="150dp"
android:layout_height="80dp"
android:id="@+id/box_fragment_1"
android:layout_below="@+id/box_fragment_2"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp"
tools:layout="@layout/fragment_box" />
<fragment
android:layout_width="150dp"
android:layout_height="50dp"
android:id="@+id/box_fragment_2"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
tools:layout="@layout/fragment_box" />
此片段的布局:
<ImageButton
android:layout_width="197dp"
android:layout_height="197dp"
android:id="@+id/ibPress"
android:layout_gravity="center" />
所以,我注册了 OnClickListener:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_box, container, false);
ImageButton imageButton= (ImageButton) view.findViewById(R.id.ibPress);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(LOG_TAG,this.toString());
//how to know,wich button clicked?
presenter.onButtonClick(...?);
}
});
return view;
}
所以,我想知道按下了哪个片段按钮?
我是否需要将 View 引用到 presenter.onButtonClick
?
在演示者,我想知道按下了什么按钮,然后做一些工作。
谢谢!
您可以使用getId()
获取当前点击视图的id
。参见下面的代码。
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.b1:
// it was the first button
break;
case R.id.b2:
// it was the second button
break;
}
}
}
请阅读 docs 了解更多信息。
您可以通过以下方式修改您的 presenter.onButtonClick()
presenter.onButtonClick(int fragmentId)
或 presenter.onButtonClick(String fragmentTagName)
然后在使用 switch case
的方法中,您可以根据该特定片段的 ID 或标签执行操作。