將listview顯示在dialog當中

一.新建一個工程

成都創(chuàng)新互聯(lián)成立以來不斷整合自身及行業(yè)資源、不斷突破觀念以使企業(yè)策略得到完善和成熟,建立了一套“以技術(shù)為基點,以客戶需求中心、市場為導(dǎo)向”的快速反應(yīng)體系。對公司的主營項目,如中高端企業(yè)網(wǎng)站企劃 / 設(shè)計、行業(yè) / 企業(yè)門戶設(shè)計推廣、行業(yè)門戶平臺運營、APP應(yīng)用開發(fā)、手機網(wǎng)站制作、微信網(wǎng)站制作、軟件開發(fā)、聯(lián)通服務(wù)器托管等實行標準化操作,讓客戶可以直觀的預(yù)知到從成都創(chuàng)新互聯(lián)可以獲得的服務(wù)效果。

    其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];//數(shù)據(jù)源

    @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();
            }
        });
    }
}

五.效果圖:

將listview顯示在dialog當中

將listview顯示在dialog當中

當前文章:將listview顯示在dialog當中
URL標題:http://muchs.cn/article46/iehieg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航App開發(fā)網(wǎng)頁設(shè)計公司、網(wǎng)站維護定制網(wǎng)站、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)