每周作业二:view

 activity_main:

<?xml version="1.0" encoding="UTF-8" ?>

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="宽度充满,高度自适应"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="限制输入的字符数为6"
        android:maxLength="6"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="只显示单行"
        android:singleLine="true" />

    <EditText
        android:id="@+id/ed5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="只能输入数字"
        android:inputType="number"/>

    <EditText
        android:id="@+id/ed6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="提示信息"/>

    <EditText
        android:id="@+id/ed7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="左侧有图片"
        android:drawableLeft="@drawable/image1"/>

    <EditText
        android:id="@+id/ed8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="文本框最初内容(含圆角矩形)"
        android:background="@drawable/shape"/>

<!--    高度自适应-->


</LinearLayout>

shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ff0000"/>
    <corners android:radius="20dp"/>
</shape>

MainActivity.java

package com.example.home2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText ed5=(EditText) findViewById(R.id.ed5);
        EditText ed6=(EditText) findViewById(R.id.ed6);
        EditText ed7=(EditText) findViewById(R.id.ed7);
        EditText ed8=(EditText) findViewById(R.id.ed8);


//        点击事件
        ed5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"只允许数字输入",Toast.LENGTH_SHORT).show();
            }
        });


//        获得焦点
        ed6.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Toast.makeText(MainActivity.this,"焦点改变",Toast.LENGTH_SHORT).show();
            }
        });


//        输入内容以后 按回车 能够反馈输入的内容
        ed7.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Toast.makeText(MainActivity.this,""+v.getText(),Toast.LENGTH_SHORT).show();
                return false;
            }
        });



//        文本内容改变
        ed8.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Toast.makeText(MainActivity.this,""+s,Toast.LENGTH_SHORT).show();
            }
//            s表示显示的内容
        });
    }
}

SecondActivity.

package com.example.home2;

import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

//    数据的保存与恢复

}

java

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值