文本视图选框不工作

我尝试使用选框,但它不工作 这是我的密码,请让我知道我哪里出错了

<TextView
android:text="lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00"
android:id="@+id/TextView02"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:singleLine="true"
android:focusable="true"
android:inputType="text"
android:maxLines="1">
</TextView>

我正在使用 android SDK 2.0.1

134193 次浏览

现在工作:) 密码附在下面

<TextView
android:text="START | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | END"
android:id="@+id/MarqueeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true">

编辑(代表阿迪尔 · 侯赛因) :

需要在代码中设置 textView.setSelected(true)才能正常工作。

为了允许滚动,这些属性必须包含在 textview标记中。

其他的都是可选的。

android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:ellipsize="marquee"

是唯一需要的属性,滚动甚至与 layout_width=0dp定义的 layout_weight一起工作

下面是一些示例代码:

<TextView
android:id="@+id/scroller"
android:singleLine="true"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
android:layout_marginLeft="4dp"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>

但是最重要的是隐式或显式的 TextView 应该会被选中

你可以这样做:

TextView txtView=(TextView) findViewById(R.id.scroller);
txtView.setSelected(true);

我遇到了同样的问题,这个讨论帮助了我,我只是更换了这一行

android:maxLines="1"

在 xml 中使用此行

android:singleLine="true"

而且成功了 线路 txtView.setSelected(true); 也在我的活动中。

工作守则:

<TextView
android:id="@+id/scroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
android:textAppearance="?android:attr/textAppearanceLarge" />
package com.app.relativejavawindow;


import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends Activity {
TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
final RelativeLayout relativeLayoutbotombar = new RelativeLayout(this);
textView = new TextView(this);
textView.setId(1);


RelativeLayout.LayoutParams relativlayparamter = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);


RelativeLayout.LayoutParams relativlaybottombar = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutbotombar.setLayoutParams(relativlaybottombar);




textView.setText("Simple application that shows how to use marquee, with a long ");
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setSelected(true);
textView.setSingleLine(true);




relativeLayout.addView(relativeLayoutbotombar);
relativeLayoutbotombar.addView(textView);
//relativeLayoutbotombar.setBackgroundColor(Color.BLACK);
setContentView(relativeLayout, relativlayparamter);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}

这个代码工作正常,但如果你的屏幕大小不填充这个文本,它将不会移动尝试排列空白的文本结束

是的,永远文本视图的固定宽度的情况下也可以工作(例如 android: lay_ width = “120dp”)

必须具备的属性包括:

  1. 可聚焦 = “ true”
  2. 机器人: focus usableInTouchMode = “ true”
  3. SingleLine = “ true”//如果缺少文本,则显示在多行中。

工作守则:

<TextView
android:id="@+id/mediaTitleTV"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="Try Marquee, it works with fixed size textview smoothly!" />

只要把这些参数放到你的 TextView 中,它就可以工作了:)

    android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"

`

只要加上上面所说的:

    android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"

必须的在你的 Java 代码中使用 TextView.setSelected (true)。

选框不能与本文中的一些家伙一起工作的原因,如果你有一个带有 EditText (这是一个输入)的输入表单,EditText 将是一个在表单中默认有焦点和选择的表单。现在,如果你强制它通过 TextView.setSelected (true) ,TextView 将最终做不管什么选框。

因此,整个想法是使 TextView 小部件集中和选择,使选框工作。

在代码中使用以下代码行:

TextView.setSelected(true);

非常简单的工作代码:

用于无限滚动文本

            <TextView
android:id="@+id/textView_News_HeadLine"
style="@style/black_extra_large_heading_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="-1"
android:singleLine="true"
android:text="HeadLine: Banglawash To be Continued" />

(你必须从你的活动中写作)

textView.setSelected(true);

我正在使用 minSDK = 14,并且很好奇这些变体的工作原理是什么:

android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"

除了其他格式的东西。我不需要滚动横向,可聚焦,或可聚焦触摸模式。

这个集合确实需要调用

setSelected(true)

我发现有趣的是 singleLine 据说已经被废弃了,建议用 maxLines = 1替换它。但是,当我这样做的时候,仅仅这个改变就会阻止文本滚动。人们希望,当 singleLine 最终被淘汰的时候,它当前的所有行为都将由 maxLines 触发... ..。

而 android: focus usableInTouchMode = “ true”是必不可少的... ..。

因为我测试了所有没有这些线的其他人,结果是没有选框。当我添加这些,它开始显示。.

我已经创建了一个自定义类 AlwaysMarqueTextView

public class AlwaysMarqueeTextView extends TextView
{
protected boolean a;


public AlwaysMarqueeTextView(Context context)
{
super(context);
a = false;
}


public AlwaysMarqueeTextView(Context context, AttributeSet attributeset)
{
super(context, attributeset);
a = false;
}


public AlwaysMarqueeTextView(Context context, AttributeSet attributeset, int i)
{
super(context, attributeset, i);
a = false;
}


public boolean isFocused()
{
return a || super.isFocused();
}


public void setAlwaysMarquee(boolean flag)
{
setSelected(flag);
setSingleLine(flag);
if(flag)
setEllipsize(TruncateAt.MARQUEE);
a = flag;
}


@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)
{
if(focused)


super.onFocusChanged(focused, direction, previouslyFocusedRect);
}


@Override
public void onWindowFocusChanged(boolean focused)
{
if(focused)
super.onWindowFocusChanged(focused);
}
}

当你想要的时候,你可以启动 Marquee

//textView.setSelected(true); No need of Selection..
textview.setAlwaysMarquee(true);

您必须添加这些属性,这是强制选框

 android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"

并在活动类中添加这行代码

textView.setSelected(true)

我也遇到过同样的问题。Amith GC 的答案(第一个被检查为接受的答案)是正确的,但是有时当文本视图不能始终获得焦点时,textview.setSelected (true) ; 不能工作。因此,为了确保 TextView 选框工作,我必须使用自定义 TextView。

public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);


}


public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);


}




@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}


@Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}




@Override
public boolean isFocused() {
return true;
}
}

然后,您可以像下面这样使用自定义的 TextView 作为布局.xml 文件中的滚动文本视图:

<com.example.myapplication.CustomTextView
android:id="@+id/tvScrollingMessage"
android:text="@string/scrolling_message_main_wish_list"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/black"
android:gravity="center"
android:textColor="@color/white"
android:textSize="15dp"
android:freezesText="true"/>

注意: 在上面的代码片段 com.example.myapplication 是一个示例包名,应该用您自己的包名替换。

希望这个能帮到你,干杯!

大部分答案都一样,
但是也要注意,在某些情况下,如果不指定容器的倾斜宽度,选框就无法工作。
例如,如果在父容器中使用重量

android:layout_width="0dp"
android:layout_weight="0.5"

帐篷可能不行。

 <TextView
android:ellipsize="marquee"
android:singleLine="true"
.../>

必须打电话报警

textView.setSelected(true);

若要拥有自己的滚动速度和自定义选框属性的灵活性,请使用以下命令:

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:lines="1"
android:id="@+id/myTextView"
android:padding="4dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Simple application that shows how to use marquee, with a long text" />

在你的活动范围内:

private void setTranslation() {
TranslateAnimation tanim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 1.0f * screenWidth,
TranslateAnimation.ABSOLUTE, -1.0f * screenWidth,
TranslateAnimation.ABSOLUTE, 0.0f,
TranslateAnimation.ABSOLUTE, 0.0f);
tanim.setDuration(1000);
tanim.setInterpolator(new LinearInterpolator());
tanim.setRepeatCount(Animation.INFINITE);
tanim.setRepeatMode(Animation.ABSOLUTE);


textView.startAnimation(tanim);
}

我已经经历过这种情况,其中文本视图选框不工作。然而,遵循这一点,我相信它将工作。:)

<TextView
android:id="@+id/tv_marquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="This is a sample code of marquee and it works"/>

并通过编程将这两行添加到..。

tvMarquee.setHorizontallyScrolling(true);
tvMarquee.setSelected(true);

SetSelected (true)是必需的,因为如果任何一个视图已经被聚焦,并且 setSelected 将使其工作。 不需要使用。

android:singleLine="true"

它是不推荐的,以上代码可以工作。