Android 常用方法


按钮样式

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="#FF9900"/>

    <corners
        android:radius="5dp"/>

shape>

solid 实体

stroke 边框 

按钮按压样式


    <item android:state_pressed="true">
        <shape>
            <solid android:color="#CC7A00" />
            <corners android:radius="10dp" />
        shape>
    item>

    <item android:state_pressed="false">
        <shape>
            <solid android:color="#FF9900" />
            <corners android:radius="10dp" />
        shape>
    item>

在button中添加点击事件

在button中添加属性

android:onClick="showToast"

之后在对应的java文件中对showToast进行声明

    public void showToast(View view){
        Toast.makeText(this,"我被点击了",Toast.LENGTH_SHORT).show();
    }

另一种设置点击事件的方法

    private Button mbtnbutton,mbtnbutton3;


        mbtnbutton3 = findViewById(R.id.btn_3);
        mbtnbutton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,edittext.class);
                startActivity(intent);
            }
        });

gravity 内容的对齐方式

设置监听事件

editText.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) {

            }
        });

charSequence为当前输入框内的内容

toolbar的设置

XML

<?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/common_toolbar"
    android:background="#ffffff"
    app:titleTextColor="#ffffff"
    app:navigationIcon="@drawable/back"
    android:layout_height="wrap_content">

JAVA

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.common_toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

    }