通过一个按钮来编辑的TextView文本的ListView内按钮、文本、编辑、TextView

2023-09-04 04:57:31 作者:姐的姿态你学不来

我有ListView.I也纷纷按钮。它们是RelativeLayout的内部。我希望能够通过按钮的onClick(视图v)更改选定的ListView项TexView文本。 ListView的布局:

I have the ListView.I have also the button. They are inside of RelativeLayout. I want to be able to change TexView text of selected ListView item via the button onClick(View v). ListView layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

</LinearLayout>

主要活动:

public class MainActivity extends Activity {
    private TextView mTextView;
    private ListView my_list;
    private MyArrayAdapter adapt;
    Button mybutton;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle); 

        String[] mContacts = {
                "Jacob Anderson dsfd sdfdf sdfsd sdfds", "Emily Duncan", "Michael Fuller", 
                "Emma Greenman", "Joshua Harrison", "Madison Johnson",
                "Matthew Cotman", "Olivia Lawson", "Andrew Chapman", 
                "Daniel Honeyman", "Isabella Jackson", "William Patterson", 
                "Joseph Godwin", "Samantha Bush", "Christopher Gatema55"};

        setContentView(R.layout.activity_main);
        my_list=(ListView)findViewById(R.id.listView1);
        adapt=new MyArrayAdapter(this, mContacts, 0);
        my_list.setAdapter(adapt);

        mybutton = (Button)findViewById(R.id.button1);
        mybutton.setOnClickListener(new OnClickListener() {     
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

我应该怎么做,如果我想编辑的TextView是这样的:

What should I do if I want to edit TextView like this:

        SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");
    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);
    TextViewOfSelectedItem.setText(text, BufferType.SPANNABLE);

如何得到ListView项的TextViewOfSelectedItem或TextView中通过项目的位置?

How can I get the TextViewOfSelectedItem or TextView of ListView item via item position?

推荐答案

您可以通过使用获得所选项目的文本 setOnItemClickListener()。您可以使用此方法。

You can get text of selected item by using setOnItemClickListener(). You can use this way.

    my_list.setOnItemClickListener(new OnItemClickListener() {

  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
 long arg3) {
         // TODO Auto-generated method stub
    String  getSelectedTextOfList = yourList.get(pos); // here you can get selected item.
 }
});
 
精彩推荐