达内课程-android入门知识(下)

语言的国际化自适应
当切换Android设备语言环境时,App中显示的文字语言自动切换
在res下创建各种需要自适应的语言对应的文件夹,例如values-zh,则当设备语言环境是中文时,会优先使用values-zh文件夹下配置的字符串资源
在values系列文件夹下用于配置的任何xml文件,都不需要关心文件名本身,只需要使用正确的节点即可。例如strings.xml文件也可以改名为任何合法的文件名,不影响正常运行

实现语言国际化自适应的文件夹名称的命名规则是“values-语言简称”,每种语言的简称是固定的,可自行百度

如果需要区分每种语言的使用的地区,则values文件夹的命名格式为“values-语言简称-r地区简称”,其中语言简称全部小写,地区简称全部大写,例如:values-zh-rCN

屏幕尺寸自适应
分辨率:形成屏幕显示的像素点,例如1024*768,表示该屏幕的水平方向有1024个像素点,垂直方向有768个,每个像素点显示不同的颜色,形成画面

在设计Android APP界面时,使用的都是dp/dip为单位,表示独立像素单位
dp与px之间的换算比例,取决于设备的显示质量

设备的显示质量,取决于设备的屏幕尺寸和分辨率
如果屏幕尺寸不大,分辨率却很高,则显示质量较高
如果屏幕尺寸较大,分辨率却一般或较低,则显示质量较低
设备的显示质量也称之为显示密度

设备的显示密度值被划分为不同的区间范围,160、240、320、480

dp与px的换算比例:

项目dppx
16011
24011.5
32012
48013

度量值自适应
使用values下的dimens.xml管理度量值,可以为不同的屏幕分辨率创建不同的values文件夹以实现度量值自适应

style样式

    <Button
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="Button"
        android:background="@drawable/button_selector"/>

改为使用style样式

在values文件夹下的style.xml文件中修改

    <style name="buttonBackground">
        <item name="android:background">@drawable/button_selector</item>
    </style>
    <Button
        style="@style/buttonBackground"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="Button"
        />

简单登录界面实现

这里写图片描述

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="用户名" />

        <EditText
            android:id="@+id/et_username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="密码" />

       <EditText
            android:id="@+id/et_password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPassword"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="100dp"
        android:layout_height="45dp"
        android:text="登录"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/button_selector"
        android:enabled="false"/>

</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    //[开发步骤]
    //1、声明控件,尽量使用private
    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //2、初始化控件
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);

        //4、创建内部类的对象,并配置"提交按钮"
        InnerOnClickListener listener = new InnerOnClickListener();
        btnLogin.setOnClickListener(listener);

        InnerTextWatcher textWatcher = new InnerTextWatcher();
        etUsername.addTextChangedListener(textWatcher);
        etPassword.addTextChangedListener(textWatcher);
    }

    //3、使用成员内部类的语法,声明内部类,实现点击监听器的接口
    private class InnerOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            //5、实现监听方法
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString();

            //常量写在左边,也许username为空
            if("admin".equals(username)&&"admin888".equals(password)){
                Toast.makeText(MainActivity.this,"登录成功#^_^#",Toast.LENGTH_SHORT).show();
                return;
            }else {
                Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                return;
            }
        }
    }

    private class InnerTextWatcher implements TextWatcher{

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString();
            btnLogin.setEnabled(username.length()>=4 && password.length()>=4);
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    }
}

button_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@drawable/shape_button_disable"/>
    <item android:state_pressed="true"
        android:drawable="@drawable/shape_button_pressed"/>
    <item
        android:drawable="@drawable/shape_button_normal"/>
</selector>

shape_button_normal

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:radius="5dp"/>
    <gradient
        android:startColor="#4169E1"
        android:endColor="#87CEFA"/>
</shape>

shape_botton_pressed

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:radius="5dp"/>
    <gradient
        android:startColor="#FF1493"
        android:endColor="#DDA0DD"/>
</shape>

shape_button_disable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:radius="5dp"/>
    <gradient
        android:startColor="#999999"
        android:endColor="#cccccc"/>
</shape>

Checkbox复选框控件

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择你的爱好"/>

    <CheckBox
        android:id="@+id/cb_coding"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="编程"/>
    <CheckBox
        android:id="@+id/cb_game"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="游戏"/>
    <CheckBox
        android:id="@+id/cb_perm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="烫头"/>
    <Button
        android:id="@+id/btn_confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定"/>
</LinearLayout>

java

public class MainActivity extends AppCompatActivity {
    CheckBox cbCoding;
    CheckBox cbGame;
    CheckBox cbPerm;
    Button btnConfirm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        cbCoding = findViewById(R.id.cb_coding);
        cbGame = findViewById(R.id.cb_game);
        cbPerm = findViewById(R.id.cb_perm);
        btnConfirm = findViewById(R.id.btn_confirm);

        InnerOnClickListener listener = new InnerOnClickListener();
        btnConfirm.setOnClickListener(listener);
    }

    private class InnerOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            String result = "";
            if(cbCoding.isChecked()){
                result+=cbCoding.getText().toString()+" ";
            }
            if(cbGame.isChecked()){
                result+=cbGame.getText().toString()+" ";
            }
            if(cbPerm.isChecked()){
                result+=cbPerm.getText().toString()+" ";
            }
            if("".equals(result)){
                Toast.makeText(MainActivity.this,"请选择至少1个爱好",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,"您选择的爱好有:"+result.trim(),Toast.LENGTH_SHORT).show();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值