AutoCompleteTextView、ListView优化、Dialog等高级组件模拟练习

模拟要求:

1、创建一个QQ的登陆界面,使用AutoCompleteTextView作用户账号;

2、登陆成功后自动跳转到B界面,通过网址http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1使用GSON解析网络数据将其展示到ListView上;

3、点击展示出来的数据会自动弹出Dialog对话框;

QQ登陆界面参考图:


 
  
 
  

首先需要用到的依赖有:

implementation 'com.squareup.okhttp3:okhttp:3.8.1'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.squareup.picasso:picasso:2.3.2'

登陆界面布局activity_login.xml文件

xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    android:gravity="center"
    android:padding="40dp"
    tools:context="com.bwie.test.zkao02.LoginActivity">
    
    <AutoCompleteTextView
        android:id="@+id/account"
        android:hint="请输入账号"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/password"
        android:layout_marginTop="10dp"
        android:layout_below="@id/account"
        android:layout_alignLeft="@id/account"
        android:hint="请输入密码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/line1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/password"
        android:layout_alignLeft="@id/password"
        android:layout_marginTop="10dp"
        >
        <CheckBox
            android:id="@+id/auto_login"
            android:text="自动登录"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/remeber_password"
            android:text="记住密码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/find_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="   找回密码"
            />
    LinearLayout>
    <Button
        android:id="@+id/login"
        android:text="登录"
        android:layout_marginTop="10dp"
        android:background="@android:color/holo_blue_bright"
        android:layout_below="@id/line1"
        android:layout_alignLeft="@id/line1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

RelativeLayout>

B界面布局activity_main.xml文件

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.bwie.test.zkao02.MainActivity">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

android.support.constraint.ConstraintLayout>


展示数据界面布局listview_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"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

LinearLayout>


用户账号的下拉选项框布局autocompletetextview_item_layout.xml文件

xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/autocompletetextview_content">

TextView>



创建登陆界面LoginActivity.java代码文件

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    //声明属性
    private AutoCompleteTextView account;
    private EditText password;
    private CheckBox auto_login;
    private CheckBox remeber_password;
    private TextView find_password;
    private Button login;
    List dataSource = new ArrayList<> ();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //初始化控件
        initView();
    }

    private void initView() {
        account = (AutoCompleteTextView) findViewById(R.id.account);
        password = (EditText) findViewById(R.id.password);
        auto_login = (CheckBox) findViewById(R.id.auto_login);
        remeber_password = (CheckBox) findViewById(R.id.remeber_password);
        find_password = (TextView) findViewById(R.id.find_password);
        login = (Button) findViewById(R.id.login);

        login.setOnClickListener(this);
        for (int i = 0; i < 10; i++) {
            dataSource.add("121947724" +i);

        }
        //为AutoCompleteTextView绑定适配器
        ArrayAdapter arrayAdapter = new ArrayAdapter(LoginActivity.this, R.layout.autocomplettextview_item_layout, dataSource);
        account.setAdapter(arrayAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpClient okHttpClient = new OkHttpClient();
                        Request request = new Request.Builder().url("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1").build();
                        try {
                            final String accountString = account.getText().toString().trim();
                            if (TextUtils.isEmpty(accountString)) {
                                Looper.prepare();
                                Toast.makeText(LoginActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                                return;
                            }
                            final String passwordString = password.getText().toString().trim();
                            if (TextUtils.isEmpty(passwordString)) {
                                Looper.prepare();
                                Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                                return;
                            }

                            Response execute = okHttpClient.newCall(request).execute();
                            ResponseBody body = execute.body();
                            byte[] bytes = body.bytes();
                            String result = new String(bytes);
                            Gson gson = new Gson();
                            Javabean javabean = gson.fromJson(result, Javabean.class);

                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtra("accountString", accountString);
                            intent.putExtra("passwordString", passwordString);
                            intent.putExtra("javabean", javabean);

                            startActivity(intent);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
        }
    }

    private void submit() {
        // validate
        String passwordString = password.getText().toString().trim();
        if (TextUtils.isEmpty(passwordString)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        String accountString = account.getText().toString().trim();
        if (TextUtils.isEmpty(accountString)) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }
    }
}
 
  

创建跳转界面MainActivity.java代码文件

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

    //声明属性
    private ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        Intent intent = getIntent();
        String accountString = intent.getStringExtra("accountString");
        String passwordString = intent.getStringExtra("passwordString");
        Javabean javabean = (Javabean) intent.getSerializableExtra("javabean");
        //为ListView绑定适配器
        ListViewAdapter listViewAdapter = new ListViewAdapter(MainActivity.this, javabean);
        list.setAdapter(listViewAdapter);
        //为ListView添加点击事件
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).create();
                alertDialog.setTitle("请确认以下讯息");//对话框标题
                alertDialog.setMessage ( "是否成功运行" );//对话框内容
                alertDialog.show();//显示对话框
            }
        });
    }

    private void initView() {
        list = (ListView) findViewById(R.id.list);
    }

    //为ListView创建适配器
    class ListViewAdapter extends BaseAdapter {

        private Context context;
        Javabean javabean;

        public ListViewAdapter(Context context, Javabean javabean) {
            this.context = context;
            this.javabean = javabean;
        }

        @Override
        public int getCount() {
            return javabean.getData().size();
        }

        @Override
        public Object getItem(int position) {
            return javabean.getData().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //优化,复用convertView
            ViewHolder viewHolder = null;
            Javabean.DataBean dataBean = javabean.getData().get(position);
            String pic = dataBean.getPic();
            String title = dataBean.getTitle();
            if (convertView == null) {//如果convertView不存在就创建一个convertView实例
                LayoutInflater layoutInflater = LayoutInflater.from(context);
                View inflate = layoutInflater.inflate(R.layout.listview_item, null);
                convertView = inflate;

                //创建viewHolder
                viewHolder = new ViewHolder(context, convertView);
                //将viewHolder保存到convertView中
                convertView.setTag(viewHolder);

            }else {//直接使用已经存储的convertView

                viewHolder = (ViewHolder) convertView.getTag();
                ImageView imageView = viewHolder.imageView;
                TextView textView = viewHolder.textView;
                Picasso.with(context).load(pic).into(imageView);
                textView.setText(title);

            }
            return convertView;
        }
    }

    class ViewHolder extends View {
        ImageView imageView;
        TextView textView;
        public ViewHolder(Context context , View view) {
            super(context);
            imageView = view.findViewById(R.id.image);
            textView = view.findViewById(R.id.text);
        }
    }
}



创建网络数据获取界面Javabean.java代码文件

import java.io.Serializable;
import java.util.List;

public class Javabean implements Serializable {

    /**
     * ret : 1
     * data : [{"id":"8289","title":"油焖大虾","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1660","food_str":"大虾 葱 生姜 植物油 料酒","num":1660},{"id":"2127","title":"四川回锅肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1587","food_str":"猪肉 青蒜 青椒 红椒 姜片","num":1587},{"id":"30630","title":"超简单芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1529","food_str":"QQ糖 牛奶 芒果","num":1529},{"id":"9073","title":"家常红烧鱼","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1420","food_str":"鲜鱼 姜 葱 蒜 花椒","num":1420},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1410","food_str":"豆腐 新鲜红椒 青椒 葱花 油","num":1410},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1338","food_str":"瘦猪肉 生菜 豆瓣酱 干辣椒 花椒","num":1338},{"id":"46968","title":"红糖苹果银耳汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1246","food_str":"银耳 苹果 红糖","num":1246},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1215","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1215},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1148","food_str":"大米 皮蛋 猪肉 油条 香葱","num":1148},{"id":"2166","title":"蚂蚁上树","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1141","food_str":"红薯粉 肉 姜 蒜 花椒","num":1141},{"id":"2262","title":"糖醋肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg","collect_num":"1075","food_str":"猪肉 红椒 黄椒 洋葱 蛋清","num":1075},{"id":"9971","title":"鱼香豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg","collect_num":"1006","food_str":"豆腐 木耳 胡萝卜 香葱 番茄酱","num":1006},{"id":"10172","title":"干煸四季豆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg","collect_num":"989","food_str":"四季豆 干辣椒 蒜头 酱油 糖","num":989},{"id":"2685","title":"胡萝卜肉末蒸蛋","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg","collect_num":"919","food_str":"胡萝卜 肉 蛋 生抽 盐","num":919},{"id":"9972","title":"虎皮青椒","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg","collect_num":"890","food_str":"青辣椒 大蒜 香醋 白糖 生抽","num":890},{"id":"10437","title":"叉烧排骨","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg","collect_num":"797","food_str":"排骨 李锦记叉烧酱 植物油 清水 油菜","num":797},{"id":"2892","title":"\u201c五行\u201d彩蔬汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg","collect_num":"757","food_str":"黑木耳 玉米 牛蒡 胡萝卜 西兰花","num":757},{"id":"2348","title":"麻辣肉丝面","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg","collect_num":"754","food_str":"面条 肉丝 淀粉 酱油 辣椒","num":754},{"id":"10044","title":"土豆炖翅根","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg","collect_num":"753","food_str":"土豆 翅根 葱 姜 料酒","num":753},{"id":"33783","title":"美人豆浆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg","collect_num":"753","food_str":"黄豆 红豆 绿豆 黑豆 黑米","num":753}]
     */

    private int ret;
    private List data;

    public int getRet() {
        return ret;
    }

    public void setRet(int ret) {
        this.ret = ret;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public static class DataBean implements Serializable{
        /**
         * id : 8289
         * title : 油焖大虾
         * pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
         * collect_num : 1660
         * food_str : 大虾 葱 生姜 植物油 料酒
         * num : 1660
         */

        private String id;
        private String title;
        private String pic;
        private String collect_num;
        private String food_str;
        private int num;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }

        public String getCollect_num() {
            return collect_num;
        }

        public void setCollect_num(String collect_num) {
            this.collect_num = collect_num;
        }

        public String getFood_str() {
            return food_str;
        }

        public void setFood_str(String food_str) {
            this.food_str = food_str;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }
}
 
  

不要忘记在清单文件AndroidManifest.xml中添加获取网络数据的权限:

<uses-permission android:name="android.permission.INTERNET"/>
以及
<activity android:name=".LoginActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    intent-filter>

activity>

最后就可以正常的运行效果了,如下图:

AutoCompleteTextView、ListView优化、Dialog等高级组件模拟练习_第1张图片

你可能感兴趣的:(AutoCompleteTextView、ListView优化、Dialog等高级组件模拟练习)