簡體   English   中英

如何在onContextItemSelected android中獲取視圖的文本

[英]how to get text of the view in onContextItemSelected android

我正在開發一個應用程序,在其中可以以編程方式創建一些標簽。 我將上下文菜單添加到每個標簽,以便當用戶單擊任何標簽時,他可以將其刪除。

我正在使用以下代碼創建標簽並注冊上下文菜單。

 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;
    }

我想在onContextItemSelected中顯示textview文本。 怎么做?

由於您將菜單項ID設置為與視圖相同的ID,因此可以使用它來定位它,即

TextView tv = (TextView)findViewById(item.getItemId());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM