我有一个ListView,其中每个项目都是TextView。 我想启用长按行为以选择部分文本,然后能够使用“文本选择”上下文操作栏“全部选择”,“复制”。我研究并尝试了各种解决方案。他们都没有为我的情况工作。
我还有一个要处理的选择弹出菜单。
有人可以请一个解决方案吗?
这是我的代码
public class MainActivity4 extends Activity
{
List<String> stringList;
ListView listView;
ArrayAdapter<String> arrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
stringList = new ArrayList<String>();
for (int i = 1; i < 21; i++) {
stringList.add("verse --" + i + " -- The Lord is my shepherd. I shall not want.");
}
registerForContextMenu(listView);
arrayAdapter = new ArrayAdapter<String>(this, R.layout.text_vew, stringList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
view.setSelected(true);
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity4.this, view);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Toast.makeText(MainActivity4.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//able to select part of list text. Example "shepherd".
// Then show Context Action Bar (CAB) that highlights selected text (example "shepherd") shows "Select All" "Copy" on the CAB
}
}
acticity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/rl"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:clickable="true"
android:descendantFocusability="blocksDescendants"/>
</RelativeLayout>
popup_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/bookmark"
android:title="bookmark"/>
<item
android:id="@+id/copyVerseToClipboard"
android:title="copy verse to clipboard"/>
<item
android:id="@+id/share"
android:title="share"/>
<item
android:id="@+id/cancel"
android:title="cancel"/>
</menu>
text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:minHeight="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20sp"
>
</TextView>