文本选框无效

时间:2013-10-29 12:44:15

标签: java android eclipse user-interface textview

请查看以下代码

<TableRow
          android:id="@+id/tableRow13"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="5dp" >

            <TextView
                 android:id="@+id/textView22"
                  android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:textColor="#ffffff"
                 android:textSize="12sp"
                 android:text="@string/r_new_event"
                 android:layout_marginLeft="5dp"
                 android:textAppearance="?android:attr/textAppearanceSmall" />

             <TextView
                 android:id="@+id/textView23"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Meeting with people i have never seen before in my entire life lol"
                 android:ellipsize="marquee"
                 android:maxLength="10"
                 android:singleLine="true"
                 android:fadingEdge="horizontal"
                 android:marqueeRepeatLimit="marquee_forever"
                 android:scrollHorizontally="true"
                 android:textColor="#ffffff"
                 android:textSize="12sp"
                 android:layout_marginLeft="10dp"
                 android:textAppearance="?android:attr/textAppearanceSmall" />

</TableRow>

如果文本超过10个字母,则尝试对文本进行选取。但不幸的是,这段代码不起作用。我试过蛾模拟器和Android手机。这有什么不对?

更新

以下是我的新代码,它仍无效

<TextView
    android:id="@+id/textView23"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Meeting with people i have never seen before in my entire life lol"
    android:ellipsize="marquee"
    android:maxLength="10"
    android:singleLine="true"
    android:fadingEdge="horizontal"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:textColor="#ffffff"
    android:textSize="12sp"
    android:layout_marginLeft="10dp"
    android:focusable="true" 
android:focusableInTouchMode="true" 
    android:freezesText="true"
    android:textAppearance="?android:attr/textAppearanceSmall" />

4 个答案:

答案 0 :(得分:3)

试试这段代码..

XML

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/mywidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:lines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ff4500"
        android:text="Simple application that shows how to use marquee, with a long text" />
</RelativeLayout>

MainActivity

public class TextViewMarquee extends Activity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) this.findViewById(R.id.tv);  
        tv.setSelected(true);  // Set focus to the textview
    }
}

答案 1 :(得分:0)

添加以下内容:

android:focusable="true" 
android:focusableInTouchMode="true" 
android:freezesText="true"

答案 2 :(得分:0)

将textView替换为以下代码

<TextView
                 android:id="@+id/textView23"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Meeting with people i have never seen before in my entire life lol"
                 android:ellipsize="marquee"
                   android:lines="1"
                 android:singleLine="true"
                 android:fadingEdge="horizontal"
                 android:marqueeRepeatLimit="marquee_forever"
                 android:scrollHorizontally="true"
                 android:textColor="#ffffff"
                 android:textSize="12sp"
                 android:layout_marginLeft="10dp"
                 android:focusable="true" 
                 android:focusableInTouchMode="true" 
                 android:freezesText="true"
                 android:textAppearance="?android:attr/textAppearanceSmall" />

答案 3 :(得分:0)

使用下面的自定义TextView / onCreate只需编写your_textView.setSelected(true);

public class MyTextView extends TextView {

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

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyTextView(Context context) {
    super(context);
}

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

}