如何在onContextItemSelected android中获取视图的文本
我正在开发一个应用程序,其中我以编程方式创建了一些标签。我向每个标签添加上下文菜单,这样当用户单击任何标签时,他都可以删除它 我使用以下代码创建标签并注册上下文菜单如何在onContextItemSelected android中获取视图的文本,android,textview,contextmenu,selecteditem,Android,Textview,Contextmenu,Selecteditem,我正在开发一个应用程序,其中我以编程方式创建了一些标签。我向每个标签添加上下文菜单,这样当用户单击任何标签时,他都可以删除它 我使用以下代码创建标签并注册上下文菜单 private void drawLabels(HashMap<String, String> contact, int position) { FlowLayout flowLayout = (FlowLayout)findViewById(R.id.recipients);
private void drawLabels(HashMap<String, String> contact, int position)
{
FlowLayout flowLayout = (FlowLayout)findViewById(R.id.recipients);
TextView contactLabel = (TextView)getLayoutInflater().inflate(R.layout.contactlabeltemplate, null);
contactLabel.setText(contact.get("name"));
contactLabel.setTag(R.id.number,String.valueOf( contact.get("number")));
contactLabel.setTag(R.id.position,Integer.valueOf(position));
flowLayout.addView(contactLabel);
registerForContextMenu(contactLabel);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
String name = ((TextView)v).getText().toString();
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(name);
menu.add(0, v.getId(), 0, "Call");
menu.add(0, v.getId(), 0, "SMS");
}
@Override
public boolean onContextItemSelected(MenuItem item){
if(item.getTitle()=="Call"){
Toast.makeText(getApplicationContext(),"text view text",Toast.LENGTH_LONG).show();
}
else if(item.getTitle()=="SMS"){
Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();
}else{
return false;
}
return true;
}
private void标签(HashMap联系人,int位置)
{
FlowLayout=(FlowLayout)findViewById(R.id.recipients);
TextView contactLabel=(TextView)GetLayoutFlater().充气(R.layout.contactlabeltemplate,null);
contactLabel.setText(contact.get(“name”));
contactLabel.setTag(R.id.number,String.valueOf(contact.get(“number”)));
contactLabel.setTag(R.id.position,Integer.valueOf(position));
flowLayout.addView(contactLabel);
registerForContextMenu(contactLabel);
}
@凌驾
public void onCreateContextMenu(ContextMenu菜单,视图v,ContextMenu.ContextMenuInfo菜单信息)
{
字符串名称=((TextView)v.getText().toString();
super.onCreateContextMenu(menu,v,menuInfo);
菜单.设置标题(名称);
添加(0,v.getId(),0,“Call”);
添加(0,v.getId(),0,“SMS”);
}
@凌驾
公共布尔值onContextItemSelected(MenuItem项){
if(item.getTitle()=“Call”){
Toast.makeText(getApplicationContext(),“文本视图文本”,Toast.LENGTH_LONG.show();
}
else if(item.getTitle()=“SMS”){
Toast.makeText(getApplicationContext(),“发送短信代码”,Toast.LENGTH_LONG.show();
}否则{
返回false;
}
返回true;
}
我想在onContextItemSelected中显示textview文本。
如何做到这一点 由于要将菜单项id设置为视图的同一id,因此可以使用该id来定位它,即
TextView tv = (TextView)findViewById(item.getItemId());