我正在制作一个演示版。因为我动态创建了ImageView
和多个TextView
。我在它们上设置了contextMenu
。一切都很好。我想要的是,当我选择contextMenu
时,如何获得所选View
的ID以执行进一步的操作..
以下是我的代码......
public class ModifyCard extends Fragment implements OnClickListener, OnTouchListener{
private AlertDialog dialog;
//Declaration of framelayout
FrameLayout f;
//Declaration of imageview
ImageView imageview;
View view;
File file;
EditText et1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_modify_card ,container, false);
captureImageInitialization();
Initialize();
return view;
}
private void Initialize() {
f=(FrameLayout)view.findViewById(R.id.framelayout);
Bundle bundle = this.getArguments();
int myInt;
myInt = bundle.getInt("position");
ImageAdapter imageAdapter = new ImageAdapter(getActivity());
f.setBackgroundResource(imageAdapter.mThumbIds[myInt]);
LinearLayout layout=new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
imageview=new ImageView(getActivity());
EditText EditTextUserName=new EditText(getActivity());
EditText EditTextUsesrMobNumber=new EditText(getActivity());
LinearLayout.LayoutParams ivparams = new LinearLayout.LayoutParams(150,150);
LinearLayout.LayoutParams EditTextUserNameParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams EditTextUsesrMobNumberParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ivparams.setMargins(0,10,10,0);
imageview.setId(1);
imageview.setBackgroundResource(R.drawable.icon_user_default);
imageview.setLayoutParams(ivparams);
EditTextUserNameParams.setMargins(10,-5,0,0);
EditTextUserName.setId(3);
EditTextUserName.setInputType(InputType.TYPE_CLASS_TEXT);
EditTextUserName.getBackground().setAlpha(0);
EditTextUserName.setHint("Enter Your Name");
EditTextUserName.setTextColor(Color.BLACK);
EditTextUserName.setLayoutParams(EditTextUserNameParams);
EditTextUsesrMobNumberParams.setMargins(10,-5,0,0);
EditTextUsesrMobNumber.setId(4);
EditTextUsesrMobNumber.setInputType(InputType.TYPE_CLASS_NUMBER);
EditTextUsesrMobNumber.getBackground().setAlpha(0);
EditTextUsesrMobNumber.setHint("Enter Mobile Number");
EditTextUsesrMobNumber.setTextColor(Color.BLACK);
EditTextUsesrMobNumber.setLayoutParams(EditTextUsesrMobNumberParams);
registerForContextMenu(imageview);
registerForContextMenu(EditTextUserName);
registerForContextMenu(EditTextUsesrMobNumber);
layout.addView(imageview);
layout.addView(EditTextUserName);
layout.addView(EditTextUsesrMobNumber);
f.addView(layout);
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if(flag==0)
{
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.context_menu, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.contextmenuedit:
Toast.makeText(getActivity(), "Edit item selected"+Integer.toString(imageview.getId()), Toast.LENGTH_SHORT).show();
break;
case R.id.contextmenudelete:
Toast.makeText(getActivity(), "Delete item selected", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
}
在ID的帮助下,我将在ImageView
和EditText
执行不同的操作。
答案 0 :(得分:1)
动态设置View
使用的ID,
view.setId(int);
要使用ID,
view.getId();
答案 1 :(得分:1)
尝试将ID
放入onContextItemSelected(MenuItem item)
,如下所示:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;