比方說,我有一個ListView
與一些項目。我想這樣做,當用戶點擊一個項目,應用程序彈出一個Toast
包含項目的名稱。例如,當用戶點擊「蘋果」時,他們會看到敬酒:「你吃蘋果。」我該怎麼做?Android ListView - 如何應對物品點擊?
2
A
回答
0
您可以設置項目點擊監聽器的列表視圖即
listvw=(ListView)findViewById(R.id.listviewid);
listvw.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Toast.makeText(ActivityName.this, "Text message", Toast.LENGTH_SHORT).show();
}
});
1
0
使用這樣的
ActivityListView的.java
package com.sunil;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ActivityListView extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] namesArray = new String[] { "Linux", "Windows7", "Eclipse",
"Suse", "Ubuntu", "Solaris", "Android", "iPhone" };
/* Create an ArrayAdapter, that will actually make the Strings above
appear in the ListView */
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, namesArray));
}
@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword,
Toast.LENGTH_SHORT).show();
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
0
試試這個代碼 listview =(ListView)findViewById(R.id.listid);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) {
// to set your list item or name
//here list is your list that you set in your adapter
list.get(pos);
}
});
相關問題
- 1. 在Widget ListView中處理物品點擊[Android]
- 2. 如何從Android中的自定義ListView中獲取點擊的物品位置?
- 3. 如何將物品點擊事件添加到物品控件
- 4. Listview點擊Android
- 5. 在ListView中點擊物品時播放聲音
- 6. 從數據庫中獲得物品的ID ListView項被點擊
- 7. Calabash android,點擊ListView?
- 8. android listview item點擊
- 9. 的Android的ListView丟失物品高度
- 10. 如何在Android中處理ListView點擊
- 11. ListView如何讓物品不顯示?
- 12. 如何在listview中爲物品設置焦點?
- 13. 對Checkbox的反應點擊Cutsom ListView
- 14. Android ListView - 如何在物品準備好時執行代碼?
- 15. 如何在android listview中獲取選定的物品索引?
- 16. 如何爲抽屜欄中的物品添加點擊事件
- 17. RecyclerView一扔和物品點擊
- 18. 模擬點擊物品使用Robolectric
- 19. iScroll不會讓物品被點擊
- 20. Android ListView小部件有時對點擊無響應
- 21. 如何避免點擊抽屜中的相同物品?
- 22. 點擊Android ListView的整行
- 23. ListView點擊在Android片段
- 24. Android Listview點擊崩潰
- 25. Android的Listview按鈕點擊
- 26. Android - SimpleCursorAdapter ListView無法點擊
- 27. Android儀器ListView點擊
- 28. OnItemSelectedListener,ListView和物品回收
- 29. 當我點擊LIstView標題時如何對Android ListView進行排序?
- 30. 爲兒童物品點擊獲取不正確的ID點擊
同樣在這裏。如何獲得按下的項目的名稱? – Arielle
你有你的列表視圖項目的視圖。只需找到視圖的文本即'TextView text =(TextView)view.findViewById(R.id.text_viewId); ' 'Toast.makeText(ActivityName.this,text.getText(),Toast.LENGTH_SHORT).show();' –
好的,謝謝。我用'TextView text =(TextView)arg1.findViewById(arg1.getId()); Toast.makeText(getApplicationContext(),text.getText(),Toast.LENGTH.SHORT).show();' – Arielle