我已经编写了这个自定义视图控件,这是我重复5次的xml。 我已经编写了onClickListener,但问题是,如何识别按下哪个按钮(有60个按钮)?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" >
<LinearLayout
android:id="@+id/LinearLayout0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@drawable/a9p_09_11_00943" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="8"
android:orientation="horizontal"
android:rowCount="4" >
<!-- prima riga P1 R1 -->
<ImageButton
android:id="@+id/btnP1C1R1"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R1"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="@drawable/add_64" android:contentDescription="@string/store_add"/>
<ImageButton
android:id="@+id/btnP1C3R1"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R1"
android:layout_width="@dimen/size_txt_view"
android:layout_column="2"
android:layout_gravity="left|center_vertical"
android:layout_row="0"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R1"
android:layout_width="@dimen/size_txt_view"
android:layout_column="4"
android:layout_gravity="left|center_vertical"
android:layout_row="0"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R1"
android:layout_width="@dimen/size_txt_view"
android:layout_column="6"
android:layout_gravity="left|center_vertical"
android:layout_row="0"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- seconda riga P1 R2 -->
<ImageButton
android:id="@+id/btnP1C1R2"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="1"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R2"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="1"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C3R2"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="1"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R2"
android:layout_column="2"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="1"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R2"
android:layout_column="4"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="1"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R2"
android:layout_column="6"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="1"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- seconda riga P1 R3 -->
<ImageButton
android:id="@+id/btnP1C1R3"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="2"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R3"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="2"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C3R3"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="2"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R3"
android:layout_column="2"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R3"
android:layout_column="4"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R3"
android:layout_column="6"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- seconda riga P1 R4 -->
<ImageButton
android:id="@+id/btnP1C1R4"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="3"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R4"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="3"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C3R4"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="3"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R4"
android:layout_column="2"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R4"
android:layout_column="4"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R4"
android:layout_column="6"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_column="0"
android:layout_row="0"
android:layout_gravity="left|center_vertical"
android:text="@string/store_4" />
<TextView
android:id="@+id/textView2"
android:layout_column="0"
android:layout_row="1"
android:layout_gravity="left|center_vertical"
android:text="@string/store_3" />
<TextView
android:id="@+id/textView3"
android:layout_column="0"
android:layout_row="2"
android:layout_gravity="left|center_vertical"
android:text="@string/store_2" />
<TextView
android:id="@+id/textView4"
android:layout_column="0"
android:layout_row="3"
android:layout_gravity="left|center_vertical"
android:text="@string/store_1" />
</GridLayout>
</LinearLayout>
</RelativeLayout>
这是coustom控制的容器:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<com.bandweb.mycpstore.Ripiano_Frigo
android:id="@+id/ripiano1"
android:tag="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.bandweb.mycpstore.Ripiano_Frigo>
<com.bandweb.mycpstore.Ripiano_Frigo
android:id="@+id/ripiano2"
android:tag="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.bandweb.mycpstore.Ripiano_Frigo>
<com.bandweb.mycpstore.Ripiano_Frigo
android:id="@+id/ripiano3"
android:tag="3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.bandweb.mycpstore.Ripiano_Frigo>
<com.bandweb.mycpstore.Ripiano_Frigo
android:id="@+id/ripiano4"
android:tag="4"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.bandweb.mycpstore.Ripiano_Frigo>
<com.bandweb.mycpstore.Ripiano_Frigo
android:id="@+id/ripiano5"
android:tag="5"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.bandweb.mycpstore.Ripiano_Frigo>
</LinearLayout>
这是控件的类:
public class Ripiano_Frigo extends LinearLayout implements android.view.View.OnClickListener{
Context Ccontext;
private static final int[] BUTTON_IDS = {
R.id.btnP1C1R1,
R.id.btnP1C1R2,
R.id.btnP1C1R3,
R.id.btnP1C1R4,
R.id.btnP1C2R1,
R.id.btnP1C2R2,
R.id.btnP1C2R3,
R.id.btnP1C2R4,
R.id.btnP1C3R1,
R.id.btnP1C3R2,
R.id.btnP1C3R3,
R.id.btnP1C3R4
};
private static final int[] TEXTVIEW_IDS = {
R.id.txtP1C1R1,
R.id.txtP1C1R2,
R.id.txtP1C1R3,
R.id.txtP1C1R4,
R.id.txtP1C2R1,
R.id.txtP1C2R2,
R.id.txtP1C2R3,
R.id.txtP1C2R4,
R.id.txtP1C3R1,
R.id.txtP1C3R2,
R.id.txtP1C3R3,
R.id.txtP1C3R4
};
private ArrayList<ImageButton> buttons = new ArrayList<ImageButton>();
private ArrayList<TextView> txtName = new ArrayList<TextView>();
public Ripiano_Frigo(Context context) {
super(context);
Ccontext = context;
loadViews();
}
public Ripiano_Frigo(Context context, AttributeSet attrs) {
super(context, attrs);
Ccontext = context;
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.ripiano_frigo, this);
loadViews();
}
private void loadViews() {
int idt=0;
for(int id : BUTTON_IDS) {
ImageButton button = (ImageButton)findViewById(id);
MyTag myTag = new MyTag(idt, BUTTON_IDS[idt],TEXTVIEW_IDS[idt], 0, 0);
// button.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View v) {
// int i = v.getId();
// final MyTag myTag = (MyTag) v.getTag();
// //if(v.getId()==BUTTON_IDS[0]){
// // v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
// // }
// Toast.makeText(Ccontext,myTag.loc+ " button "+v.getId() , Toast.LENGTH_LONG).show();
// }
// });
buttons.add(button);
setMyTag(myTag, idt);
buttons.get(idt).setOnClickListener(this);
idt++;
}
for(int id : TEXTVIEW_IDS) {
txtName.add((TextView)findViewById(id));
}
}
public void setTxtName(String name, int index) {
if((index < 0)||(index > TEXTVIEW_IDS.length))
return;
txtName.get(index).setText( name );
}
public void setMyTag(MyTag myTag, int index) {
if((index < 0)||(index > BUTTON_IDS.length))
return;
buttons.get(index).setTag(myTag);
}
public void setClickListener(OnClickListener listener){
for(int id = 0; id < BUTTON_IDS.length; id++) {
buttons.get(id).setOnClickListener(listener);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Ccontext, " button "+v.getId() , Toast.LENGTH_LONG).show();
}
}
结果是这样的:
在主要活动中我使用此代码创建控件:
Ripiano_Frigo Ripiano1;
Ripiano_Frigo Ripiano2;
Ripiano1 = (Ripiano_Frigo)rootView.findViewById(R.id.ripiano1);
Ripiano2 = (Ripiano_Frigo)rootView.findViewById(R.id.ripiano2);
Ripiano1.setTxtName("crema",0);
Ripiano2.setTxtName("cioccolato",5);
但是当我点击左边的第一个按钮(第一个组)时,返回信息是正确的ID,第一个组的所有按钮都正确响应。 单击相同的按钮,但对于下面的第二组,返回消息等于第一个按钮(R.id.btnP1C1R1)。
如何识别按下哪个按钮?
EDIT 我试过这个解决方案,我已经将android:tag =“1”添加到第一个自定义控件中,依此类推到另一个,
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
com.bandweb.mycpstore.Ripiano_Frigo rip1 = (com.bandweb.mycpstore.Ripiano_Frigo)findViewById(R.id.ripiano1);
com.bandweb.mycpstore.Ripiano_Frigo rip2 = (com.bandweb.mycpstore.Ripiano_Frigo)findViewById(R.id.ripiano2);
com.bandweb.mycpstore.Ripiano_Frigo rip3 = (com.bandweb.mycpstore.Ripiano_Frigo)findViewById(R.id.ripiano3);
com.bandweb.mycpstore.Ripiano_Frigo rip4 = (com.bandweb.mycpstore.Ripiano_Frigo)findViewById(R.id.ripiano4);
com.bandweb.mycpstore.Ripiano_Frigo rip5 = (com.bandweb.mycpstore.Ripiano_Frigo)findViewById(R.id.ripiano5);
if(rip1 != null)
Toast.makeText(Ccontext, "1 - button "+v.getId() , Toast.LENGTH_LONG).show();
else if(rip2 != null)
Toast.makeText(Ccontext, "2 - button "+v.getId() , Toast.LENGTH_LONG).show();
else if(rip3 != null)
Toast.makeText(Ccontext, "3 - button "+v.getId() , Toast.LENGTH_LONG).show();
else if(rip4 != null)
Toast.makeText(Ccontext, "4 - button "+v.getId() , Toast.LENGTH_LONG).show();
else if(rip5 != null)
Toast.makeText(Ccontext, "5 - button "+v.getId() , Toast.LENGTH_LONG).show();
}
是一招......
修改 这是主类,在这个类中我想为按钮实现onclick
public class Page2 extends Fragment
{
private static final String TAG = "Pagina2";
View rootView, rootViewList;
Context appContext,GappContext;
Context appContextDialog;
Ripiano_Frigo Ripiano1;
Ripiano_Frigo Ripiano2;
@Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
public void onCreate(Bundle outState) {
super.onCreate(outState);
Log.d(TAG, "onCreate step 0");
// TODO Put your application initialization code here.
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.page2, container, false);
Log.d(TAG, "onCreateView step 1");
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated step 3");
super.onActivityCreated(savedInstanceState);
appContext = getActivity().getBaseContext();
init(rootView, savedInstanceState);
}
void init(View rootView, Bundle savedInstanceState){
Ripiano1 = (Ripiano_Frigo)rootView.findViewById(R.id.ripiano1);
Ripiano2 = (Ripiano_Frigo)rootView.findViewById(R.id.ripiano2);
Ripiano1.setTxtName("crema",0);
Ripiano2.setTxtName("cioccolato",5);
//Ripiano1.setClickListener(this);
Ripiano1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("CLICKED!");
}
});
} //********************* end init****************************
}
但点击时只能获取视图事件而非按钮事件... 怎么能在这里得到它?
答案 0 :(得分:0)
将标签添加到xml布局中的按钮而不是代码。
这样既可以更容易地保持标签,也可以提高性能。
然后,在onClick处理程序中,您可以执行v.getTag()来读取按钮的标记,并使用标记来标识按钮。