android?studio實(shí)驗(yàn):?UI設(shè)計(jì)?ListView及事件響應(yīng)
實(shí)驗(yàn)?zāi)康模?/strong>
- 1.學(xué)習(xí)使用
ListView
及其事件響應(yīng)方法。 - 2.閱讀
sdk/doc/index
脫機(jī)文檔或者在Android developer
開(kāi)發(fā)者網(wǎng)站上,學(xué)習(xí)使用彈出菜單PopupMenu
及其事件響應(yīng)方法。
實(shí)驗(yàn)要求:
- 1.實(shí)現(xiàn)一個(gè)列表,其中顯示班級(jí)學(xué)號(hào)姓名,提供添加功能
- 2.對(duì)添加的選項(xiàng),通過(guò)長(zhǎng)按某項(xiàng),調(diào)用彈出菜單顯示刪除功能,并刪除該選項(xiàng)。
package com.example.shiyan3; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.PopupMenu; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.ContextMenu; import android.view.Gravity; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { List<String> list; ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText t1=(EditText) findViewById(R.id.edit_1); EditText t2=(EditText) findViewById(R.id.edit_2); EditText t3=(EditText) findViewById(R.id.edit_3); ListView listView = (ListView)findViewById(R.id.listview); Button bt= (Button) findViewById(R.id.button); list = new ArrayList<String>(); list.add("班級(jí) 姓名 學(xué)號(hào)"); adapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,list); listView.setAdapter(adapter); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { list.add(t1.getText().toString()+" "+t2.getText().toString()+" "+t3.getText().toString()); adapter.notifyDataSetChanged(); } }); AdapterView.OnItemLongClickListener lclistener=new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { popupshow(view,i); adapter.notifyDataSetChanged(); return false; } }; listView.setOnItemLongClickListener(lclistener); } public void popupshow(View v,int i){//自定義函數(shù) PopupMenu popup=new PopupMenu(this,v); popup.setGravity(Gravity.CENTER_HORIZONTAL); MenuInflater inflater=popup.getMenuInflater(); inflater.inflate(R.menu.popupmenu,popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { if(item.getItemId()==R.id.delete) { list.remove(i); adapter.notifyDataSetChanged(); } return false; } }); if(i!=0) popup.show();//防止把第一行給刪了 } }
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <TableRow android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="班級(jí):" android:textSize="20sp" /> <EditText android:id="@+id/edit_1" android:layout_height="wrap_content" android:layout_width="500px" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" android:textSize="20sp" /> <EditText android:id="@+id/edit_2" android:layout_width="500px" android:layout_height="wrap_content" android:text="" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="學(xué)號(hào):" android:textSize="20sp" /> <EditText android:id="@+id/edit_3" android:layout_width="500px" android:layout_height="wrap_content" android:text="" /> </TableRow> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="350px" android:layout_marginRight="350px" android:text="添加" /> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </TableLayout>
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/delete" android:title="刪除"/> <item android:id="@+id/add" android:title="添加"/> </menu>
到此這篇關(guān)于android studio實(shí)驗(yàn): UI設(shè)計(jì) ListView及事件響應(yīng)的文章就介紹到這了,更多相關(guān)UI設(shè)計(jì) ListView及事件響應(yīng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
從源碼分析Android的Glide庫(kù)的圖片加載流程及特點(diǎn)
這篇文章主要介紹了從源碼分析Android的Glide庫(kù)的圖片加載流程及特點(diǎn),Glide庫(kù)是Android下一款人氣很高的多媒體資源管理庫(kù),特別是在處理gif加載方面受到眾多開(kāi)發(fā)者青睞,需要的朋友可以參考下2016-04-04Android使用TabLayou+fragment+viewpager實(shí)現(xiàn)滑動(dòng)切換頁(yè)面效果
這篇文章主要介紹了Android使用TabLayou+fragment+viewpager實(shí)現(xiàn)滑動(dòng)切換頁(yè)面效果,需要的朋友可以參考下2017-05-05如何使用Flutter開(kāi)發(fā)一款電影APP詳解
這篇文章主要給大家介紹了關(guān)于如何使用Flutter開(kāi)發(fā)一款電影APP的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Android 處理OnItemClickListener時(shí)關(guān)于焦點(diǎn)顏色的設(shè)置問(wèn)題
這篇文章主要介紹了Android 處理OnItemClickListener時(shí)關(guān)于焦點(diǎn)顏色的設(shè)置問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-02-02Android中ScrollView 滑到頭部或尾部可伸縮放大效果
最近做項(xiàng)目遇到這樣的需求S當(dāng)crollView 滑動(dòng)到頂部,不能在滑動(dòng)的時(shí)候,圖片可以下拉放大,松開(kāi)又恢復(fù),滑到底部沒(méi)有內(nèi)容的時(shí)候,也有伸縮效果,下面通過(guò)實(shí)例代碼給大家介紹Android ScrollView 滑到頭部或尾部可伸縮放大功能,一起學(xué)習(xí)吧2017-03-03AndroidStudio Gradle基于友盟的多渠道打包方法
這篇文章主要介紹了AndroidStudio Gradle基于友盟的多渠道打包方法,需要的朋友可以參考下2017-09-09android 退出程序解決內(nèi)存釋放的問(wèn)題
做Android項(xiàng)目的時(shí)候發(fā)現(xiàn)一個(gè)問(wèn)題:當(dāng)應(yīng)用程序退出了,點(diǎn)擊"設(shè)置"查看應(yīng)用程序,界面顯示著可以點(diǎn)擊"強(qiáng)制關(guān)閉 由于這個(gè)問(wèn)題我發(fā)現(xiàn)了一個(gè)更加嚴(yán)重的問(wèn)題,那就是,在我應(yīng)用程序退出之后,系統(tǒng)并沒(méi)有釋放掉我應(yīng)用程序所占內(nèi)存2012-11-11