前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发

Android开发

作者头像
用户8447427
发布2022-08-18 16:28:01
1.6K0
发布2022-08-18 16:28:01
举报
文章被收录于专栏:userlyz学习记录userlyz学习记录

项目创建

项目文件的功能

app/src/main/java java代码

app/src/main/res 放一些资源文件

app/src/main/res/drawable 放置一些图片或者一些自定义的xml文件

app/src/main/res/layout 主要放置一些布局文件

app/src/main/res/mipmap-hdpi启动的图片或者logo放置在这个文件夹里

app/src/main/res/mipmap-mdpi

app/src/main/res/mipmap-xhdpi

app/src/main/res/mipmap-xxhdpi

app/src/main/res/mipmap-xxxhdpi

app/src/main/res/values 使用到的颜色呀文字呀

app/src/main/AndriodMainfext.xml 清单文件 我们应用中所使用到的所有activity都要在这里声明和注册

布局管理器

线性布局 LinearLayout

最常用的属性

android:id 相当于一个表示 android:layout_margin 外边距

android:layout_width 宽度 android:layout_padding 内边距

android:layout_height 高度 android:orientation 方向只有在特定的布局才会有

android:background 背景 android:gravity居中


方向: vertical纵向; horizontal横向

android:layout_weight这个所占的比重

activity_main.xml

记得改前面的<<LinearLayout xmlns…..

代码语言:javascript
复制
<LinearLayout
              android:id="@+id/accessibility_custom_action_11"
              android:layout_width="200dp"
              android:layout_height="200dp"
              android:orientation="vertical"
              android:background="#000000"
              android:layout_marginBottom="15dp"
              android:paddingLeft="20dp"
              android:paddingBottom="10dp"
              android:paddingRight="20dp"
              android:paddingTop="100dp">
    <View
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#FF0033"/>
</LinearLayout>
<LinearLayout
              android:layout_width="400dp"
              android:layout_height="200dp"
              android:orientation="horizontal"
              android:background="#0066ff"
              android:layout_marginLeft="5dp"
              android:layout_marginRight="5dp"
              >
    <View
          android:layout_width="200dp"
          android:layout_height="match_parent"
          android:background="#000000"/>
    <View
          android:layout_width="200dp"
          android:layout_height="match_parent"
          android:background="#00FF7F"/>
</LinearLayout>
<LinearLayout
              android:layout_marginTop="5dp"
              android:layout_width="match_parent"
              android:layout_height="200dp"
              android:orientation="horizontal"
              android:background="#0066ff"
              android:layout_marginLeft="0dp"
              android:layout_marginRight="0dp"
              >
    <View
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:background="#000000"
          android:layout_weight="1"/>
    <View
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:background="#00FF7F"
          android:layout_weight="1"/>
</LinearLayout>
效果
效果

相对布局RalativeLayout

除了线性布局几种还有这些特有的

android:layout_toLeftOf 谁的左边 android:layout_below在谁的下边

android:layout_toRightOf在谁的右边

android:layout_alignBottom跟谁底部对齐

android:layout_alignParentBottom跟父空间底部对齐

记得改前面的<<RelativeLayout xmlns…

代码语言:javascript
复制
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/view_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:background="#000000" />

    <View
        android:id="@+id/view_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000" />

    <View
        android:id="@+id/view_3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_toRightOf="@id/view_2"
        android:background="#000000" />

    <View
        android:id="@+id/view_4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@id/view_2"
        android:background="#00ff00" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@id/view_4"
        android:background="#40E0D0"
        android:padding="15dp">
        <View
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#4169E1"/>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            android:padding="10dp">
            <View
                android:id="@id/view_11"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#ff9900"/>
            <View
                android:id="@id/view_12"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#ff9900"
                android:layout_toRightOf="@id/view_11"
                android:layout_marginLeft="10dp"/>
            
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>
效果
效果

组件

Textview

文字大小,颜色

代码语言:javascript
复制
android:textColor="#000000"
android:textSize="30sp" 

显示不下的问题

使用android:ellipsize=”end”显示不下会在后边显示…

文字+icon

代码语言:javascript
复制
<TextView
    android:id="@+id/tv_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableRight="@drawable/icon_arrow_off"
    android:drawablePadding="10dp"
    android:text="筛选"
    android:textColor="#000000"
    android:textSize="30sp" />

中划线,下划线

代码语言:javascript
复制
mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中划线
mTv4.getPaint().setAntiAlias(true);//去掉锯齿
mTv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
mTv6=findViewById(R.id.tv_6);
mTv6.setText(Html.fromHtml("<u>userlyz</u>"));//下划线
xml
    <TextView
        android:id="@+id/tv_6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#000000"
        android:textSize="30sp"
        android:layout_marginTop="10dp"/>

跑马灯

代码语言:javascript
复制
<TextView
    android:id="@+id/tv_7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="userlyzuserlyzuserlyzuserlyzuserlyzuserlyz"
    android:textColor="#000000"
    android:textSize="30sp"
    android:layout_marginTop="10dp"
    android:singleLine="true"//单行显示
    android:ellipsize="marquee"//设置跑马灯样式
    android:marqueeRepeatLimit="marquee_forever"//跑马灯运行时间
    android:focusable="true"
    android:focusableInTouchMode="true"/>

Button

文字大小,颜色

代码语言:javascript
复制
<Button
    android:id="@+id/btn_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffff00"
    android:text="按钮1"
    android:textColor="#ff0000"
    android:textSize="20sp" />
效果
效果

自定义背景形状

圆角

drawable/bg_button2.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#ff9900"
        />
    <corners
        android:radius="15dp"/>

</shape>

layout/activity_button.xml

代码语言:javascript
复制
<Button
    android:id="@+id/btn_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/btn_1"
    android:layout_marginTop="10dp"
    android:background="@drawable/bg_button2"
    android:text="按钮2"
    android:textColor="#ff0000"
    android:textSize="20sp" />
圆角
圆角

自定义按压效果

bg_button4.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#ffff00"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#000000"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>


</selector>

activity_button.xml

代码语言:javascript
复制
<Button
    android:id="@+id/btn_4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/btn_3"
    android:layout_marginTop="10dp"
    android:background="@drawable/bg_button4"
    android:text="按钮4触摸效果"
    android:textColor="#ff99"
    />

点击事件 两种方法

点击事件基本适用于所有的组件

在activity_button.xml中添加一行onclick

代码语言:javascript
复制
android:onClick="showToast"

在ButtonActivity中编写showToast方法

遇到的问题,没有加参数,导致识别不到showToast方法

代码语言:javascript
复制
public void showToast(View view){
    Toast.makeText(this,"我被点击了",Toast.LENGTH_SHORT).show();
}
点击事件效果
点击事件效果

上面这种是一种不常用的方法

常用的方法还还是

ButtonActivity

代码语言:javascript
复制
private Button mbut3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_button);
    mbut3=findViewById(R.id.btn_3);
    mbut3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(ButtonActivity.this,"button3我被点击了",Toast.LENGTH_SHORT).show();
        }
    });
Button3被点击
Button3被点击
TextView被点击效果
TextView被点击效果

EditText

可以输入的一个控件

常用属性

代码语言:javascript
复制
<?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"
    tools:context=".EditTextActivity">
    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
        android:maxLines="1"
        android:textSize="20sp"
        android:textColor="#EEEE11"
        android:background="@drawable/bg_username"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:drawableStart="@drawable/username"
        android:drawablePadding="5dp"
        />

    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_1"
        android:layout_marginTop="10dp"
        android:height="50dp"
        android:background="@drawable/bg_username"
        android:hint="密码"
        android:maxLines="1"
        android:inputType="textPassword"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#09F768"
        android:textSize="30sp"
        android:drawableLeft="@drawable/password"
        android:drawablePadding="5dp"/>
    <EditText
        android:id="@+id/et_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:height="50dp"
        android:textSize="20sp"
        android:textColor="#3D11EE"
        android:layout_below="@id/et_2"
        android:layout_marginTop="15dp"
        android:hint="手机号"
        android:inputType="number"
        android:background="@drawable/bg_username"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"/>

    <Button
        android:id="@+id/Edit_but"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_3"
        android:text="登录"
        android:layout_marginTop="15dp"
        android:textColor="#000000"
        android:textSize="30sp"
        android:background="@drawable/bg_button_edit_l"/>

</RelativeLayout>

监听事件

主要是对于editText内容的监听

代码语言:javascript
复制
public class EditTextActivity extends AppCompatActivity {

    private Button mbut4;
    private EditText mEdirUsername;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text);
        mbut4=findViewById(R.id.Edit_but);
        mbut4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(EditTextActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();
            }
        });
        mEdirUsername=findViewById(R.id.et_1);
        mEdirUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.d("edittext",charSequence.toString());//输出当前内容
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }
}

制作登录界面

登录界面
登录界面

RadioButton

自定义样式

上为默认样式下面是自定义样式

运行结果
运行结果

android:button=”@null”是消除前面的圆圈,但是还是与选中效果的

android:checked=”true”设置默认选择,前提是要设置好组内的所有的RadioButton的id

代码语言:javascript
复制
<?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"
    tools:context=".RadioButtonActivity">
    <RadioGroup
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"
        android:layout_height= "wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:checked="true"
            android:textSize="20sp"
            android:textColor="#00FF00"/>
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="20sp"
            android:textColor="#000000"/>
    </RadioGroup>
    <RadioGroup
        android:id="@+id/rg_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/rg_1"
        android:layout_marginTop="50dp"
        >
        <RadioButton
            android:id="@+id/rb_3"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:text="男"
            android:button="@null"
            android:checked="true"
            android:textSize="30sp"
            android:textColor="#ffffff"
            android:gravity="center"
            android:background="@drawable/bg_radiobutton"/>
        <RadioButton
            android:id="@+id/rb_4"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:text="女"
            android:gravity="center"
            android:button="@null"
            android:textSize="30sp"
            android:textColor="#ffffff"
            android:background="@drawable/bg_radiobutton"
            android:layout_marginTop="10dp"/>

    </RadioGroup>


</RelativeLayout>

监听事件

RadioButtonActivity

代码语言:javascript
复制
public class RadioButtonActivity extends AppCompatActivity {
    private RadioGroup mRg1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        mRg1=findViewById(R.id.rg_1);
        mRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton=radioGroup.findViewById(i);
                Toast.makeText(RadioButtonActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

复选框CheckBox

经典

代码语言:javascript
复制
<CheckBox
    android:id="@+id/cb_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/tv_title"
    android:text="android"
    android:textSize="25sp" />

自定义样式

代码语言:javascript
复制
<CheckBox
    android:id="@+id/cb_5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="编程"
    android:textSize="20sp"
    android:layout_marginTop="15dp"
    android:paddingLeft="10dp"
    android:button="@drawable/bg_checkbox"
    />

bg_checkbox

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/unchosed"/>
    <item android:state_checked="true" android:drawable="@drawable/chosed"/>
</selector>

监听事件

代码语言:javascript
复制
mcb5=findViewById(R.id.cb_5);
mcb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        Toast.makeText(CheckBoxActivity.this, b?"5选中":"5未选中", Toast.LENGTH_SHORT).show();
    }
});

ImageView

Button的其他衍生控件:ToggleButton,Switch

常用属性

android:src 图片控件中的图片来源

android:scaleType图片的抻拉模式,图片有可能会被拉伸

fitXY:撑满控件,宽高比可能发生变化

fitCenter:保持宽高比缩放,直至能够完全显示

centerCrop:保持宽高比,直至完全覆盖控件,裁剪显示

加载网络图片

在bulid.gradle添加

代码语言:javascript
复制
repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.13.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'
}

这样会报错,解决方法在下面问题中有解

在ImageViewActivity中

load(图片的网路地址).into(插入的位置)

代码语言:javascript
复制
mIv3=findViewById(R.id.imv_3);
Glide.with(this).load("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").into(mIv3);

注意要添加INTERNET权限

列表视图ListView

常用属性

listSelector设置点击时候的样式

divider用于设置两个listview中间的条的样式

dividerHeight用于设置中间条的样式

Adapter接口 设置一个适配器 用于设置每一条目的内容

代码语言:javascript
复制
public class MylistAdapter extends BaseAdapter {
    private Context mcontext;
    private LayoutInflater mlayoutInflater;

    public MylistAdapter(Context context){
        this.mcontext=context;
        mlayoutInflater=LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return 10;//数据的长度
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

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


    static class ViewHolder{
        public ImageView imageView;
        public TextView tv_title,tv_time,tv_content;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //定义每一行长得什么样子
        ViewHolder viewHolder=null;
        if(view==null){
            view=mlayoutInflater.inflate(R.layout.layout_list_item,null);
            viewHolder=new ViewHolder();
            viewHolder.imageView=view.findViewById(R.id.iv);
            viewHolder.tv_title=view.findViewById(R.id.litem_title);
            viewHolder.tv_time=view.findViewById(R.id.litem_time);
            viewHolder.tv_content=view.findViewById(R.id.litem_content);
            view.setTag(viewHolder);
        }
        else{
            viewHolder= (ViewHolder) view.getTag();
        }
        //给控件赋值
        viewHolder.tv_title.setText("这是标题");
        viewHolder.tv_time.setText("2088-08-80");
        viewHolder.tv_content.setText("这是新换的内容");
        Glide.with(mcontext).load("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").into(viewHolder.imageView);
        return view;
    }
}

Demo演示

点击或者长按会有响应事件

效果
效果

网格视图GridView

android:numColumns=”3”每行的item数 android:horizontalSpacing=”10dp”列边距 android:verticalSpacing=”15dp”行边距

大致和上面的ListView差不多,都是要设置Adapter

结果也是差不多的

运行结果
运行结果

滚动视图ScrollView

垂直滚动ScrollView

水平滚动HorizontalScrollView

控件中的直接子元素只能有一个

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Button
                    android:layout_width="200dp"
                    android:layout_height="300dp"
                    android:text="Test"
                    android:textAllCaps="false"/>
                <Button
                    android:layout_width="200dp"
                    android:layout_height="300dp"
                    android:text="Test"
                    android:textAllCaps="false"/>
                <Button
                    android:layout_width="200dp"
                    android:layout_height="300dp"
                    android:text="Test"
                    android:textAllCaps="false"/>
            </LinearLayout>
        </HorizontalScrollView>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test"
            android:textAllCaps="false"
            android:layout_marginTop="8000dp"/>
    </LinearLayout>
</ScrollView>

RecyclerView

RecyclerView能够灵活实现大数据集的展示,视图的复用管理比ListView更好,能够显示列表、网格、瀑布流等形式,且不同的ViewHolder能够实现item的多元化的功能

但是使用起来会稍微麻烦一点,并且没有类似于ListView的onItemClickListener监听事件,需要开发者自己实现

LinearLayoutManager

两个item中间并没有下划线,要想有下滑线要自己进行绘制创建ItemDecoration,间隔颜色不设置就是背景颜色,也可以自己设置

LinearRecyclerViewActivity

代码语言:javascript
复制
public class LinearRecyclerViewActivity extends AppCompatActivity {
    private RecyclerView mRvMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linear_recycler_view);
        mRvMain=findViewById(R.id.rv_main);
        mRvMain.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this));
        mRvMain.addItemDecoration(new MyDecoration());
        mRvMain.setAdapter(new MyLinearAdapter(LinearRecyclerViewActivity.this));
    }
    //创建下划线,这个并不只可以创建下划线
    class MyDecoration extends RecyclerView.ItemDecoration{
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state){
            super.getItemOffsets(outRect,view,parent,state);
            outRect.set(0,0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight));
        }
    }
}

MyLinearAdapter

代码语言:javascript
复制
public class MyLinearAdapter extends RecyclerView.Adapter<MyLinearAdapter.LinrearViewHolder> {
    private Context mcontext;
    private List<String> list;
    public MyLinearAdapter(Context context){
        this.mcontext=context;
    }
    @NonNull
    @Override
    public MyLinearAdapter.LinrearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new LinrearViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.layout_linear_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyLinearAdapter.LinrearViewHolder holder, int position) {
        holder.textView.setText("hello world");
        //设置点击事件
         holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mcontext, "click...."+position, Toast.LENGTH_SHORT).show();
            }
        });
         holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(mcontext, "longclick---"+position, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return 30;
    }
    class LinrearViewHolder extends RecyclerView.ViewHolder{
        private TextView textView;
        public LinrearViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.linear_tv_title);
        }
    }
}

activity_linear_recycler_view.xml和layout_linear_item.xml

代码语言:javascript
复制
activity_linear_recycler_view.xm   
	<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/gray"/>
layout_linear_item.xml
    <TextView
        android:id="@+id/linear_tv_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:textColor="@color/white"
        android:background="@color/black"
        android:textSize="25sp"/>

点击事件也可以这样做

LinearRecyclerView

代码语言:javascript
复制
    mRvMain.setAdapter(new MyLinearAdapter(LinearRecyclerViewActivity.this, new MyLinearAdapter.OnItemClickListener() {
        @Override
        public void onclick(int pos) {
            Toast.makeText(LinearRecyclerViewActivity.this, "click"+pos, Toast.LENGTH_SHORT).show();
        }
    }));
}
最终效果
最终效果

GridLayoutManager

StaggeredGridLayoutManager

WebView]

问题

安卓应用布局Missing Constraints in ConstraintLayout错误解决方法

安卓权限管理

按照直接在bulid.gradle添加glide依赖时候报错解决办法

添加glide

自己创建activity,不使用一键创建的empty activity

创建在包目录下创建java class文件

代码语言:javascript
复制
package com.example.first.ListView;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.example.first.R;

public class ListViewActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
    }
}

在layout下创建对应的布局文件

创建布局文件
创建布局文件

在androidMainfest.xml下声明创建的acticity

代码语言:javascript
复制
<activity
    android:name=".ListView.ListViewActivity"
    android:exported="false"/>

Android 连接数据库

代码语言:javascript
复制
new  Thread(){
    public  void run(){
        try {
            Class.forName("com.mysql.jdbc.Driver");//动态加载类
            String url = "127.0.0.1:3306/public";
            //上面语句中 mysql://mysql.lianfangti.top为你的mysql服务器地址 3306为端口号   public是你的数据库名 根据你的实际情况更改
            Connection conn = DriverManager.getConnection(url, "root", "root");
            //使用 DriverManger.getConnection链接数据库  第一个参数为连接地址 第二个参数为用户名 第三个参数为连接密码  返回一个Connection对象
            if(conn!=null){ //判断 如果返回不为空则说明链接成功 如果为null的话则连接失败 请检查你的 mysql服务器地址是否可用 以及数据库名是否正确 并且 用户名跟密码是否正确
                Log.d("调试","连接成功");
                Statement stmt = conn.createStatement(); //根据返回的Connection对象创建 Statement对象
                String sql = "select * from user"; //要执行的sql语句
                ResultSet rs = stmt.executeQuery(sql); //使用executeQury方法执行sql语句 返回ResultSet对象 即查询的结果
            }else{
                Log.d("调试","连接失败");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}.start();
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目创建
  • 项目文件的功能
  • 布局管理器
    • 线性布局 LinearLayout
      • 相对布局RalativeLayout
      • 组件
        • Textview
          • Button
            • EditText
              • RadioButton
                • 复选框CheckBox
                  • ImageView
                    • 列表视图ListView
                      • 网格视图GridView
                        • 滚动视图ScrollView
                          • RecyclerView
                            • LinearLayoutManager
                            • GridLayoutManager
                            • StaggeredGridLayoutManager
                          • WebView]
                          • 问题
                            • 按照直接在bulid.gradle添加glide依赖时候报错解决办法
                              • 自己创建activity,不使用一键创建的empty activity
                              • Android 连接数据库
                              领券
                              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档