Android:week 8总结

目录

Monday:

1.AlertDialog

Tuesday:

1.ListView


Monday:

1.AlertDialog

      AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。

思路:定义相关类(AlertDialog),对需要处理的方法设置接口。

(1)创建布局文件,在app—>res—>layout下定义布局文件

custom_alertdialog.xml 相关代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:singleLine="true"
        android:gravity="center"
        android:visibility="visible">
    </TextView>

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:gravity="center">
    </TextView>
    <View
        android:layout_width="match_parent"
        android:layout_height="2sp"
        android:background="#888888">
    </View>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/negtive"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="15sp">
        </Button>
<!--        添加竖线-->
        <View
            android:layout_width="2sp"
            android:layout_height="match_parent"
            android:background="#888888">
        </View>
        <Button
            android:id="@+id/positive"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="15sp">
        </Button>
    </LinearLayout>
</LinearLayout>

(2)创建java文件,在app—>java—>和MainActivity在同一个文件夹下

CustomAlertDialog.java  相关代码

package com.example.week9;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

public class CustomAlertDialog extends AlertDialog {

    private TextView title,message;
    private Button negtive,positive;
    private String strTitle,strMessage;//接受从外传来的字符串信息
    private String strNegtive,strPositive;

    public CustomAlertDialog(Context context){
        super(context);
    }
    public interface OnClickListener{
        public void onPositiveClick();//确定键
        public void onNegtiveClick();//取消键
    }
    OnClickListener onClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_alertdialog);
        title = findViewById(R.id.title);
        message = findViewById(R.id.message);
        positive = findViewById(R.id.positive);
        negtive = findViewById(R.id.negtive);
        //设置监听事件
        positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onPositiveClick();
            }
        });
        negtive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onNegtiveClick();
            }
        });
    }

    public CustomAlertDialog setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    public CustomAlertDialog setTitle(String title) {
        this.strTitle = title;
        return this;
    }
    public CustomAlertDialog setMessage(String message) {
        this.strMessage = message;
        return this;
    }
    public CustomAlertDialog setPositive(String positive) {
        this.strPositive = positive;
        return this;
    }
    public CustomAlertDialog setnegtive(String negtive) {
        this.strNegtive = negtive;
        return this;
    }

    //显示信息
    public void show(){
        super.show();
        //设置标题
        if(!TextUtils.isEmpty(strTitle))  //如果标题不为空 则显示
            title.setText(strTitle);
        //设置消息
        if(!TextUtils.isEmpty(strMessage)) {
            message.setText(strMessage);
        }
        //取消键
        if(!TextUtils.isEmpty(strNegtive)) {
            negtive.setText(strNegtive);
        }
        else
            negtive.setText("取消");
        //确定键
        if(!TextUtils.isEmpty(strPositive)) {
            positive.setText(strPositive);
        }
        else
            positive.setText("确定");
    }
}

 

 

 

Tuesday:

1.ListView

activity_main.xml 相关代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

<!--    android:listSelector  选中颜色改变-->
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="#aaaaaa"
        android:divider="#aaaaaa"
        android:dividerHeight="2sp"
        android:scrollbars="none">
    </ListView>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java 相关代码

package com.example.week9_tuesday;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.lv);
        listView.setAdapter(new Listview(this));
    }
}

custom_view.xml 相关代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/information"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp">
        </TextView>
        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="#ff0000"/>
    </LinearLayout>
</LinearLayout>

Listview.java 相关代码:

package com.example.week9_tuesday;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

//继承适配器
public class Listview extends BaseAdapter {
    Context context;
    private String[] tag={"jiafa1","jiafa2","jiafa3","jiafa4","jiafa5","jiafa6"};
    private String[] prices={"$:100元","$:200元","$:300元","$:200元","$:100元","$:200元"};
    private int[] icons = {R.mipmap.jiafa,R.mipmap.jiafa,R.mipmap.jiafa,R.mipmap.jiafa,R.mipmap.jiafa,R.mipmap.jiafa};
    //构造函数
    public Listview(Context context) {
        this.context = context;
    }

    //需要重载四个方法
    @Override

    //总共多少个需要显示
    public int getCount(){
        Log.d("adapter","getCount:"+tag.length+"\n");
        return tag.length;
    }
    //返回指定View的相关信息
    public Object getItem(int position){
        Log.d("adapter","getItem:"+tag[position]+"\n");
        return tag[position];
    }
    //返回id值
    public long getItemId(int position){
        return 0;
    }
    //创建一个View
    public View getView(int position, View converView, ViewGroup parent){  //converView 当前滑出的view  parent父布局
        View view = View.inflate(context,R.layout.custom_view,null);
        ImageView image = view.findViewById(R.id.image);
        TextView infoemation = view.findViewById(R.id.information);
        TextView price = view.findViewById(R.id.price);
        image.setBackgroundResource(icons[position]);//设置图片信息
        //设置名称 相关介绍
        infoemation.setText(tag[position]);
        price.setText(prices[position]);

        Log.d("adapter","getView:"+tag[position]+" "+prices[position]+"\n");
        return  view;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值