TextView,如何向用户显示提示

TextView, how to display prompt for user

我需要为我的 TextView 设置提示,如果 String 太长无法放入 TV(我设置的最大长度为 20),那么它只显示它的一部分,最后带有“...”。当我点击电视时,我想显示带有完整字符串的提示。可能吗?如果是,那么该怎么做?

里面 activity:

textCompany.setText(testDb.getCompanyName(id));
textCompany.setEllipsize(null);

和XML:

 <TextView
            android:id="@+id/textCompany"
            android:layout_width="match_parent"
            android:layout_below="@+id/textId"
            android:layout_height="wrap_content"
            android:maxLength="20"
            android:ellipsize="end"
            android:gravity="end"
            android:layout_marginEnd="20dp"
            android:textSize="17sp"

            android:text="verylongstringjusttotestifthisworksandletshopeitwill" />

您提到的行为是由于

android:椭圆="end"

如果不适合宽度,它会在文本末尾显示 "Some text..."。 您可以通过编程将椭圆大小值更改为 none 以在单击时显示全文。

myTextView.setEllipsize(null);

您可以使用简单的 onClickListener 来做到这一点。首先,检查文本的长度,如果超过 20 个字符,则取前 20 个并在末尾添加三个点并显示。同时,您将全文保存到一个临时变量中,并在有人单击您的 TextView 时显示它。

         @Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

            final TextView textView = findViewById(R.id.textCompany);
            String shortMessage;
            final String message = textView.getText().toString();
            if(message.length() >= 20){
                shortMessage = message.substring(0,19)+"...";
                textView.setText(shortMessage);
            }
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();

                }
            });

        }

注意:通过将以下内容添加到您的 xml 确保您的 textView 可点击:

android:clickable="true"