Listview项目自动点击

huangapple go评论76阅读模式
英文:

Listview Items auto click

问题

I have translated the code parts as requested:

我之前发布过这个问题但目前还无法解决我们如何通过按钮强制列表视图项目依次自动点击我的目标是使列表视图中的所有项目依次可点击并执行某些操作例如向其他人发送 WhatsApp 消息
放置文本到列表视图的代码如下

button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(!namesText.getText().toString().isEmpty()){
            adapterNames.add(namesText.getText().toString());
            namesText.setText("");
            adapterNames.notifyDataSetChanged();
        }
    }
});

发送 WhatsApp 消息的列表视图代码如下

listNamesId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
       // 这两行用于获取项目的索引值
        //adapterNames.getItem(pos);
        //Toast.makeText(MainActivity.this, "saif = " + pos, Toast.LENGTH_SHORT).show();

        //以下代码用于获取项目文本值
        View nextItem = listNamesId.getChildAt(pos+1);
        if(nextItem != null){
            String selectedFromList = (String) (listNamesId.getItemAtPosition(pos));
            namesText.setText(selectedFromList);
        }
        if (namesTexter.getText().toString().trim().length() == 0) {
            Toast.makeText(MainActivity.this, "您忘记填写目标号码!", Toast.LENGTH_SHORT).show();

        } else {
            //注意:请首先使用国家代码的前2位数字(不包括加号)
            try {
                namesTexter.setText(listNamesId.getItemAtPosition(pos).toString());
                String mobile = namesTexter.getText().toString();
                String msg = textmessage.getText().toString();
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://api.whatsapp.com/send?phone=" + mobile + "&text=" + msg)));

            } catch (Exception e) {
                // WhatsApp 应用程序未安装
            }

        }
    }
});

请注意,我只翻译了代码的部分,没有包含其他内容。如果需要进一步的帮助,请随时提问。

英文:

I have posted this question before but was unable to resolve the issue as of now. How can we force list view items to be auto clicked in turn through Button? My goal is to make all items in the list view be clickable in turn and perform certain actions such s send Whats app message to others.
Code of putting Text into Listview is.

    button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!namesText.getText().toString().isEmpty()){
adapterNames.add(namesText.getText().toString());
namesText.setText(&quot;&quot;);
adapterNames.notifyDataSetChanged();
}
}
});

Listview Code for Sending Whats app message is:

    listNamesId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView&lt;?&gt; list, View v, int pos, long id) {
// This two lines is for get indexing value of an item
//adapterNames.getItem(pos);
//Toast.makeText(MainActivity.this, &quot;saif = &quot; + pos, Toast.LENGTH_SHORT).show();
//this below code is for getting item text value
View nextItem = listNamesId.getChildAt(pos+1);
if(nextItem != null){
String selectedFromList = (String) (listNamesId.getItemAtPosition(pos));
namesText.setText(selectedFromList);
}
if (namesTexter.getText().toString().trim().length() == 0) {
Toast.makeText(MainActivity.this, &quot;You are missing your destination number!&quot;, Toast.LENGTH_SHORT).show();
} else {
//NOTE : please use with country code first 2digits without plus signed
try {
namesTexter.setText(listNamesId.getItemAtPosition(pos).toString());
String mobile = namesTexter.getText().toString();
String msg = textmessage.getText().toString();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(&quot;https://api.whatsapp.com/send?phone=&quot; + mobile + &quot;&amp;text=&quot; + msg)));
} catch (Exception e) {
//whatsapp app not install
}
}
}
});

My Task is as shown in image
Listview项目自动点击

答案1

得分: 0

请查看以下已翻译的代码部分:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt_all_displaying_items"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Auto Click All Displaying items" />

    <Button
        android:id="@+id/bt_all_list_items"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Auto Click All List items" />

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Testing"
        android:textSize="30sp" />

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    final static int delayTime = 250;

    ArrayList<String> listSample = new ArrayList<>();
    Button bt1, bt2;
    EditText et;
    ListView lv;
    int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = findViewById(R.id.bt_all_displaying_items);
        bt2 = findViewById(R.id.bt_all_list_items);
        et = findViewById(R.id.et);
        lv = findViewById(R.id.lv);

        setupSampleList();
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listSample);
        lv.setAdapter(adapter);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                i = 0;
                Handler handler = new Handler();
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        lv.performItemClick(lv.getChildAt(i), i + lv.getFirstVisiblePosition(), 0);
                        i++;
                        if (i < (lv.getChildCount())) handler.postDelayed(this, delayTime);
                    }
                };
                handler.postDelayed(runnable, delayTime);
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                i = 0;
                lv.smoothScrollToPosition(i);
                Handler handler = new Handler();
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        lv.performItemClick(lv.getChildAt(i - lv.getFirstVisiblePosition()), i, 0);
                        i++;
                        if (i < listSample.size()) {
                            lv.smoothScrollToPosition(i);
                            handler.postDelayed(this, delayTime);
                        } else {
                            Toast.makeText(getBaseContext(), "End of List!", Toast.LENGTH_LONG).show();
                        }
                    }
                };
                handler.postDelayed(runnable, delayTime);
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView textView = (TextView) view;
                textView.setText(textView.getText() + ": " + et.getText());
                textView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText((String) lv.getItemAtPosition(i));
                    }
                }, 200);
            }
        });
    }

    private void setupSampleList() {
        for (int i = 0; i < 50; i++) listSample.add("Sample Item: " + (i + 1));
    }
}

希望这些翻译对您有所帮助。如果您需要进一步的帮助,请随时提出。

英文:

Try this sample:

activity_main.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;Button
android:id=&quot;@+id/bt_all_displaying_items&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Auto Click All Displaying items&quot; /&gt;
&lt;Button
android:id=&quot;@+id/bt_all_list_items&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Auto Click All List items&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/et&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Testing&quot;
android:textSize=&quot;30sp&quot; /&gt;
&lt;ListView
android:id=&quot;@+id/lv&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;/LinearLayout&gt;

MainActivity.java:

public class MainActivity extends AppCompatActivity {
final static int delayTime = 250;
ArrayList&lt;String&gt; listSample = new ArrayList&lt;&gt;();
Button bt1, bt2;
EditText et;
ListView lv;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = findViewById(R.id.bt_all_displaying_items);
bt2 = findViewById(R.id.bt_all_list_items);
et = findViewById(R.id.et);
lv = findViewById(R.id.lv);
setupSampleList();
ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_1, listSample);
lv.setAdapter(adapter);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
i = 0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
lv.performItemClick(lv.getChildAt(i), i + lv.getFirstVisiblePosition(), 0);
i++;
if (i &lt; (lv.getChildCount())) handler.postDelayed(this, delayTime);
}
};
handler.postDelayed(runnable, delayTime);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
i = 0;
lv.smoothScrollToPosition(i);
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
lv.performItemClick(lv.getChildAt(i - lv.getFirstVisiblePosition()), i, 0);
i++;
if (i &lt; listSample.size()) {
lv.smoothScrollToPosition(i);
handler.postDelayed(this, delayTime);
} else {
Toast.makeText(getBaseContext(), &quot;End of List!&quot;, Toast.LENGTH_LONG).show();
}
}
};
handler.postDelayed(runnable, delayTime);
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
TextView textView = (TextView) view;
textView.setText(textView.getText() + &quot;: &quot; + et.getText());
textView.postDelayed(new Runnable() {
@Override
public void run() {
textView.setText((String)lv.getItemAtPosition(i));
}
}, 200);
}
});
}
private void setupSampleList() {
for (int i = 0 ; i &lt; 50; i++) listSample.add(&quot;Sample Item: &quot; + (i + 1));
}
}

huangapple
  • 本文由 发表于 2023年6月12日 18:54:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定