欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

安卓开发学习,基础入门介绍

程序员文章站 2022-07-05 09:35:21
安卓b站学习笔记梳理2020.11.25天哥在奔跑安卓教程b站网址https://www.bilibili.com/video/BV1Rt411e764.Button①文字大小、颜色②自定义背景形状③自定义按压效果④点击事件点击事件几乎适用于所有组件⑤⑥⑦⑧⑨......

安卓b站学习笔记梳理2020.11.25

天哥在奔跑安卓教程b站网址https://www.bilibili.com/video/BV1Rt411e76

目录

安卓b站学习笔记梳理2020.11.25

4.Button

①文字大小、颜色

②自定义背景形状

③自定义按压效果

④点击事件第一种方法

⑤点击事件第二种方法

5.EditText

①常用属性(以用户名输入框为例)

②监听事件

6.0MainActivity中提高代码复用的做法

7.RadioButton

①常用属性(创建RadioGroup并且在下面新建RadioButton)

②自定义样式(取消圆圈并且实现点击有其他样式)xml如下

③监听事件



4.Button

①文字大小、颜色

<Button
 android:id="@+id/btn_2"
 android:layout_width="match_parent"
 android:layout_height="40dp"
 android:text="Button2"
 android:textSize="20sp"
 android:textColor="#FFFFFF"
 android:background="@drawable/bg_btn2"
 android:layout_below="@id/btn_1"
 android:layout_marginTop="10dp"
    />

②自定义背景形状

  1. drawable->new drawable resources file->shape(Root element)-------(实心背景颜色+圆角)
    <?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>
  2. drawable->new drawable resources file->shape(Root element)-------(空心背景颜色+圆角)
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <stroke
            android:width="3dp"
            android:color="#FF9900"/>
        <corners
            android:radius="15dp"/>
    </shape>
  3. 然后在button中的 android:background后面赋值"@drawable/bg_btn2",就可以实现绑定

③自定义按压效果

drawable->new drawable resources file->selector(Root element)-------(按压效果)

<?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="#AA6600"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#FF9900"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>

④点击事件第一种方法

点击事件几乎适用于所有组件

  1. BUTTON中添加
    android:onClick="showToast"
  2. 在ButtonAcitivity中加入
    public void showToast(View view)
    {
        Toast.makeText(this,"Button4 is clicked!",Toast.LENGTH_SHORT).show();
    }
  3. 就可以实现点击button后下方弹出默认圆角消息弹框

⑤点击事件第二种方法

  1. 在ButtonActivity中注册Button或者TextView
    private Button mBtn3;
    private TextView mtv8;
  2. 在ButtonActivity中的OnCreate()下绑定、添加监听事件
    mBtn3 = findViewById(R.id.btn_3);
    mBtn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ButtonActivity.this,"Button3 is clicked!",Toast.LENGTH_SHORT).show();
        }
    });
    
    mtv8 = findViewById(R.id.tv_8);
    mtv8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ButtonActivity.this,"TextView8 is clicked!",Toast.LENGTH_SHORT).show();
        }
    });
  3. 点击Button或者TextView同样弹出默认圆角消息弹框

5.EditText

①常用属性(以用户名输入框为例)

实现出来是左边drawableLeft一个图片

<EditText
    android:id="@+id/et_1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="16sp"
    android:textColor="#FFAD33"
    android:hint="User Name"
    android:background="@drawable/bg_username"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:drawableLeft="@drawable/user"
    android:drawablePadding="5dp"
    android:maxLines="1"
    android:layout_marginTop="50dp"
    />

然后如果是限制输入只能是数字的话加入下面这行代码:

<!--    android:inputType="number"-->

掩盖密码:

android:inputType="textPassword"

②监听事件

按钮点击事件(先注册):

mBnLogin = findViewById(R.id.btn_login);
mBnLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(EditTextActivity.this,"Login successfully!",Toast.LENGTH_SHORT).show();
    }
});

文本修改监听事件(同样先注册):

mEtUserName = findViewById(R.id.et_1);
mEtUserName.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.d("Edittext",s.toString());
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

6.MainActivity中提高代码复用的做法

  1. OnCreate()中只保留绑定和“setListener()”的调用
    public class MainActivity extends AppCompatActivity {
        private Button mBtnTextView;
        private Button mBtnButton;
        private Button mBtEditText;
        private Button mBtnRadioButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mBtnTextView = findViewById(R.id.btn_textview);
            mBtnButton = findViewById(R.id.btn_Button);
            mBtEditText = findViewById(R.id.btn_edittext);
            mBtnRadioButton = findViewById(R.id.btn_radiobutton);
            setListener();
        }
  2. MainActivity中新建Onclick接口类
    private class Onclick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch (v.getId()) {
                case R.id.btn_textview:
                    intent = new Intent(MainActivity.this,TextViewActivity.class);
                    break;
                case R.id.btn_Button:
                    intent = new Intent(MainActivity.this,ButtonActivity.class);
                    break;
                case R.id.btn_edittext:
                    intent = new Intent(MainActivity.this,EditTextActivity.class);
                    break;
                case R.id.btn_radiobutton:
                    intent = new Intent(MainActivity.this,RadioButtonActivity.class);
                    break;
            }
            startActivity(intent);
        }
    }
  3. 新建setListener()函数来新建Onclick类,并且对于每一个button使用setOnclickListener();
    private void setListener()
    {
        Onclick onclick = new Onclick();
        mBtnTextView.setOnClickListener(onclick);
        mBtnButton.setOnClickListener(onclick);
        mBtEditText.setOnClickListener(onclick);
        mBtnRadioButton.setOnClickListener(onclick);
    
    }

7.RadioButton

①常用属性(创建RadioGroup并且在下面新建RadioButton)

<RadioGroup
    android:id="@+id/rg_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton
        android:id="@+id/rb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="man"
        android:checked="true"
        android:textSize="18sp"
        android:textColor="#FF6600"/>
    <RadioButton
        android:id="@+id/rb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="woman"
        android:textSize="18sp"
        android:textColor="#FF6600"/>
</RadioGroup>

②自定义样式(取消圆圈并且实现点击有其他样式)xml如下

<RadioGroup
    android:id="@+id/rg_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    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:gravity="center"
        android:text="man"
        android:button="@null"
        android:background="@drawable/bg_btn4"
        android:checked="true"
        android:textSize="18sp"
        android:textColor="#FF6600"/>
    <RadioButton
        android:id="@+id/rb_4"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="woman"
        android:button="@null"
        android:background="@drawable/bg_btn4"
        android:textSize="18sp"
        android:textColor="#FF6600"
        android:layout_marginLeft="10dp"/>
</RadioGroup>

drawable文件的xml如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <solid android:color="#AA6600"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:state_checked="false">
        <shape>
            <stroke android:width="1dp"
                android:color="#AA6600"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>

③监听事件

实现点击弹出Toast

public class RadioButtonActivity extends AppCompatActivity {

    private RadioGroup RG1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);

        RG1 = findViewById(R.id.rg_1);
        RG1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = group.findViewById(checkedId);
                Toast.makeText(RadioButtonActivity.this,radioButton.getText(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

本文地址:https://blog.csdn.net/weixin_42898615/article/details/110142096