Bri*_*ian 13 android textview android-layout
我正在开发Android应用程序(API 15及更低版本).在我的UI中,我有一个TextView元素,我希望人们能够从中进行选择和复制.这是我的元素的样子:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp" />
<TextView
android:id="@+id/chat_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:padding="8dp" />
<TextView
android:id="@+id/chat_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:padding="8dp"
android:textSize="18sp"
android:gravity="right"
android:textColor="@color/BLACK"
android:textIsSelectable="true"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
此TextView位于使用SimpleCursorAdapter填充的ListView中.这个ListView看起来像这样:
<ListView
android:id="@+id/chat_text_display"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:padding="5dp"
android:background="@color/WHITE"
android:divider="@null"
android:divider_height="0dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"/>
Run Code Online (Sandbox Code Playgroud)
ListView在LinearLayout中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/custom_border">
<ListView
android:id="@+id/chat_text_display"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:padding="5dp"
android:background="@color/WHITE"
android:divider="@null"
android:dividerHeight="0dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
android:layout_marginTop="2dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp">
<Button
android:id="@+id/text_send"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:layout_gravity="center_vertical"
android:enabled="false"
android:text="@string/chat_button"/>
<EditText
android:id="@+id/chat_text_compose"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:minLines="3"
android:maxLines="3"
android:paddingLeft="8dp"
android:background="@color/WHITE"
android:hint="@string/chat_hint"/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
每当我尝试单击chat_info或chat_message中的文本时,都不会发生任何事情.但是,每当我尝试双击文本时:
在"工具栏"中,这是我看到的:

它看起来像是复制对话框,但它会立即消失.
我可以使用a在LinearLayout中插入一个"独立"TextView,android:textIsSelectable="true"并且复制对话框可以正常工作; 这意味着它会一直保持可见,直到我选择"复制".
我可以提供的最后一条信息是我的LinearLayout在使用带有ViewPager的片段的选项卡式活动中.我只是看不出这是怎么回事,因为就像我说的那样,我可以在LinearLayout中添加另一个元素(例如TextView),并且复制对话框可以完美地运行.我想我会添加这篇文章以获得完全清晰.
我想要做的就是选择chat_info或chat_message中的文本,这样我就可以将文本复制并粘贴到其他地方.
有任何想法吗?
新的消息!!!
当我在"chat_text_compose"EditText中选择文本时,复制工具栏会正常显示.此时,我可以在TextView区域中成功选择文本.奇怪的.
更新:控制此布局的代码
public class Chat extends Fragment {
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.chat, container, false);
return rootView;
}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
displayChats();
}
@Override
public void onResume() {
super.onResume();
displayChats();
}
@Override
public View getView() {
final Button sendChatButton = (Button) rootView.findViewById(R.id.text_send);
final EditText chatEntryWindow = (EditText) rootView.findViewById(R.id.chat_text_compose);
// Check to see if the text entry field is empty. If it is empty, disable the "Send" button.
chatEntryWindow.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() != 0){
sendChatButton.setEnabled(true);
} else {
sendChatButton.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {}
});
// Send the chat
if(sendChatButton != null) {
sendChatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Date rightNow = new Date();
SimpleDateFormat timeSDF = new SimpleDateFormat(Constants.SIMPLE_TIME, Locale.US);
SimpleDateFormat dateSDF = new SimpleDateFormat(Constants.SIMPLE_DATE, Locale.US);
SharedPreferences myAppPreferences = getContext().getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
String message = chatEntryWindow.getText().toString();
String username = myAppPreferences.getString("username", Constants.TABLET_ID);
Message myMessage = new Message(true, username, message, 0, dateSDF.format(rightNow), timeSDF.format(rightNow));
if(!message.equals("")){
LogChat logChat = new LogChat(getActivity());
logChat.addNewMessage(myMessage);
new SendChat(getActivity(), message, username).execute();
chatEntryWindow.setText("");
sendChatButton.setEnabled(false);
if(v != null){
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
displayChats();
}
}
});
}
return super.getView();
}
public void displayChats(){
DatabaseHelper myDBHelper = new DatabaseHelper(getActivity());
final Cursor chatsCursor = myDBHelper.getChatsCursor();
String[] fromColumns = {"messageInfo","messageText"};
int[] toViews = {R.id.chat_information, R.id.chat_message};
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.line_of_chat, chatsCursor, fromColumns, toViews, 0);
ListView myListView = (ListView) rootView.findViewById(R.id.chat_text_display);
// Draw the list
myListView.setAdapter(simpleCursorAdapter);
myDBHelper.close();
}
}
Run Code Online (Sandbox Code Playgroud)
仅供参考,以防万一其他人来寻找答案。
我放弃了尝试这种方式,使用“android:textIsSelectable='true'”,并走了另一条路。相反,我设置了一个“onItemLongClickListener()”并使用 ClipboardManager 来抓取聊天中的文本:
public void displayChats(){
final Context context = getContext();
final ClipboardManager clipboardManager = (ClipboardManager)context.getSystemService(context.CLIPBOARD_SERVICE);
DatabaseHelper myDBHelper = new DatabaseHelper(getActivity());
final Cursor chatsCursor = myDBHelper.getChatsCursor();
String[] fromColumns = {"messageInfo","messageText"};
int[] toViews = {R.id.chat_information, R.id.chat_message};
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.line_of_chat, chatsCursor, fromColumns, toViews, 0);
ListView myListView = (ListView) rootView.findViewById(R.id.chat_text_display);
// Draw the list
myListView.setAdapter(simpleCursorAdapter);
myListView.setOnLongClickListener(new AdapterView.OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
TextView chatText = (TextView)view.findViewById(R.id.chat_message);
ClipData clipData = ClipData.newPlainText("copied chat", chatText.getText.toString());
if(clipboardManager.hasPrimaryClip()){
Toast.makeText(this, "copied text", Toast.LENGTH_LONG).show();
}
return false;
}
});
myDBHelper.close();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
598 次 |
| 最近记录: |