我有一个线性布局,其中包含三个列表视图和三个按钮。我已经定制了列表视图元素。我只希望如果有人触摸列表视图的元素,则相应的文本视图应作为选取框生效。

主要的线性布局如下-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<ListView
    android:id="@+id/list1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    android:layout_marginTop="5dip"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:textSize="16dip" />

<Button
    android:id="@+id/more_button1"
    android:layout_width="130dip"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    android:layout_marginTop="5dip"
    android:text="More" >
</Button>

<ListView
    android:id="@+id/list2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    android:layout_marginTop="5dip"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:textSize="16dip" />

<Button
    android:id="@+id/more_button2"
    android:layout_width="130dip"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    android:layout_marginTop="5dip"
    android:text="More" >
</Button>

<ListView
    android:id="@+id/list3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    android:layout_marginTop="5dip"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:textSize="16dip" />

<Button
    android:id="@+id/more_button3"
    android:layout_width="130dip"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    android:layout_marginTop="5dip"
    android:text="More" >
</Button>

</LinearLayout>


自定义列表项如下(list_item_1,list_item_2,list_item_3)-

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:freezesText="true"
android:lines="1"
android:marqueeRepeatLimit="marquee_forever"
android:padding="10dp"
android:scrollHorizontally="true"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:singleLine="true"
android:textSize="20sp" >

</TextView>


活动如下-

public class MarqueeActivity extends Activity {

ListView lv1 = null;
ListView lv2 = null;
ListView lv3 = null;

String s1[] = {"Hello This is a long Text which will help in tsting", "How", "Are", "You"};
String s2[] = {"I", "Am", "Fine"};
String s3[] = {"Welcome", "In", "New", "World"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lv1 = (ListView) findViewById(R.id.list1);
    lv2 = (ListView) findViewById(R.id.list2);
    lv3 = (ListView) findViewById(R.id.list3);

    lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_1, s1));
    lv2.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_2, s2));
    lv3.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_3, s3));

    }
}


例如如果活动开始,然后有人触摸了第一个列表视图中的第一个元素(您好,这是一个长文本,将有助于测试),则它应该开始选取框。

提前致谢!!

最佳答案

这有效:

lv1.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {
            // TODO Auto-generated method stub
            Log.e("Child at 0 pos"," "+lv1.getChildAt(pos));
            txtv = (TextView)lv1.getChildAt(pos);
            txtv.setSelected(true);

        }
    });

09-28 15:02