zoukankan      html  css  js  c++  java
  • 第六次作业

    所需截图:

    所需代码:

    1):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    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=".MainActivity">
    <ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    </LinearLayout>

    2):
    <?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">

    <LinearLayout
    android:layout_margin="18dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
    android:id="@+id/iv"
    android:layout_width="60dp"
    android:layout_height="60dp" />

    <TextView
    android:id="@+id/Text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:textSize="35sp"
    android:layout_marginLeft="10dp"/>
    </LinearLayout>
    </LinearLayout>

    3):
    package com.example.myapplication;

    import androidx.appcompat.app.AppCompatActivity;

    import android.os.Bundle;
    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 android.widget.Toast;

    import org.w3c.dom.Text;

    public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private String arr[]={"京东商城","QQ","QQ斗地主","新浪微博","天猫","UC浏览器","微信"};
    private int icons[] = new int[]{R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView=findViewById(R.id.listView);

    MyBaseAdapter myBaseAdapter=new MyBaseAdapter();
    listView.setAdapter(myBaseAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(MainActivity.this,"你点击的是第"+(position+1)+"个条目", Toast.LENGTH_SHORT).show();
    }
    });

    }
    protected class MyBaseAdapter extends BaseAdapter {
    @Override
    public int getCount() {
    return arr.length;
    }

    @Override
    public Object getItem(int position) {
    return arr[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View view=View.inflate(MainActivity.this,R.layout.list_item,null);
    ImageView imageView=view.findViewById(R.id.iv);
    TextView textView=view.findViewById(R.id.Text);
    imageView.setBackgroundResource(icons[position]);
    textView.setText(arr[position]);
    return view;
    }
    }
    }
     
  • 相关阅读:
    问卷调查--来自20145320周岐浩
    第一次尝试编写java
    写在开始编写Java之前(2)——Java的环境
    写在开始编写Java之前(1)——Java的跨平台性
    一步一步实现JS拖拽插件
    序列图像三维重建 学习流水账
    linux配置虚拟机网络环境(老师要求的host-only)
    递归函数时间复杂度分析(转)
    什么是static?什么是final?
    复习java数据库操作的总结
  • 原文地址:https://www.cnblogs.com/369236941jp/p/11644476.html
Copyright © 2011-2022 走看看