您好,登錄后才能下訂單哦!
一.新建一個工程
其activity_main.xml文件如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.webjet.listviewondialog.MainActivity"> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="50dp" android:text="" android:gravity="center" /> <Button android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="30dp" android:background="#00ff00" android:text="點擊顯示對話框" android:onClick="go" /> </LinearLayout>
二.定義一個listview的xml文件
這里定義為listview.xml具體代碼如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="20dp" android:paddingRight="20dp" android:orientation="vertical" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
三.定義listview的item的xml文件
這里定義為list_item.xml具體代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="20dp" /> </LinearLayout>
四.編寫MainActivity
具體代碼如下:
import android.app.AlertDialog; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { ArrayAdapter adapter ; AlertDialog.Builder builder; AlertDialog alertDialog; TextView tv_name;//main中的 tv用來接受所選擇的的值 TextView tv;//listview中的tv String[] data = new String[20];//數據源 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 for(int i=0; i<20; i++){ data[i] = "小豬"+i; } tv = (TextView) findViewById(R.id.tv); tv_name = (TextView) findViewById(R.id.tv_name); } public void go(View v){ builder = new AlertDialog.Builder(this); LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View viewRelatedType = inflater.inflate(R.layout.listview,(ViewGroup)findViewById(R.id.ll)); ListView listView = (ListView) viewRelatedType.findViewById(R.id.lv); listView.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,R.id.tv,data)); builder.setView(viewRelatedType); alertDialog = builder.show(); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { tv_name.setText(data[i]); alertDialog.dismiss(); } }); } }
五.效果圖:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。