1月13日 EditText

Posted on 2021-01-13 13:12  ***Pepsi***  阅读(24)  评论(0编辑  收藏  举报

EditText

通过学习editText的使用做了一个简单的登录界面

 1 package com.example.firstapp;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.content.Context;
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.text.Editable;
 9 import android.text.TextWatcher;
10 import android.util.Log;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.EditText;
14 import android.widget.Toast;
15 
16 import java.util.Map;
17 
18 public class EditTextActivity extends AppCompatActivity {
19     private Button mBtnLogin;
20     private EditText mEtUserName;//声明空间
21     private EditText editname;
22     private EditText editpasswd;
23     private String strname;
24     private String strpasswd;
25     private SharedHelper sh;
26     private Context mContext;
27 
28     @Override
29     protected void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(R.layout.activity_edit_text);
32         mContext = getApplicationContext();
33         sh = new SharedHelper(mContext);
34 
35         mBtnLogin = findViewById(R.id.btn_login);
36         editname=findViewById(R.id.et_1);
37         editpasswd=findViewById(R.id.et_2);
38         mBtnLogin.setOnClickListener(new View.OnClickListener()
39         {
40             @Override
41             public void onClick(View v){
42                 strname=editname.getText().toString();
43                 strpasswd=editpasswd.getText().toString();
44                 sh.save(strname,strpasswd);
45                 Toast.makeText(EditTextActivity.this,"登录成功!",Toast.LENGTH_SHORT).show();
46                 Intent intent = new Intent(EditTextActivity.this,MainActivity.class);
47                 startActivity(intent);
48             }
49         });
50         mEtUserName = findViewById(R.id.et_1);//监听事件
51         mEtUserName.addTextChangedListener(new TextWatcher() {
52             @Override
53             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
54 
55             }
56 
57             @Override
58             public void onTextChanged(CharSequence s, int start, int before, int count) {
59                 Log.d("edittext",s.toString());
60             }
61 
62             @Override
63             public void afterTextChanged(Editable s) {
64 
65             }
66         });
67     }
68     @Override
69     protected void onStart() {
70         super.onStart();
71         Map<String,String> data = sh.read();
72         editname.setText(data.get("username"));
73         editpasswd.setText(data.get("passwd"));
74     }
75 }

layout

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3 
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:padding="15dp">
 7 
 8     <EditText
 9         android:id="@+id/et_1"
10         android:layout_width="match_parent"
11         android:layout_height="50dp"
12         android:layout_marginTop="25dp"
13         android:background="@drawable/bg_username"
14         android:drawableLeft="@drawable/yonghum"
15         android:drawablePadding="7dp"
16         android:hint="用户名:"
17         android:maxLines="1"
18         android:paddingLeft="10dp"
19         android:textSize="16sp" />
20 
21     <EditText
22         android:id="@+id/et_2"
23         android:layout_width="match_parent"
24         android:textSize="16sp"
25         android:hint="密码:"
26         android:inputType="textPassword"
27         android:layout_below="@id/et_1"
28         android:layout_height="50dp"
29         android:drawableLeft="@drawable/mimasuo"
30         android:maxLines="1"
31         android:layout_marginTop="25dp"
32         android:drawablePadding="7dp"
33         android:background="@drawable/bg_username"
34         android:paddingLeft="10dp"/>
35     <Button
36         android:id="@+id/btn_login"
37         android:layout_width="match_parent"
38         android:layout_height="40dp"
39         android:layout_below="@id/et_2"
40         android:layout_marginTop="25dp"
41         android:text="登录"
42         android:textSize="25sp"
43         android:textColor="#fff"
44         android:background="@drawable/bg_btn4"/>
45 </RelativeLayout>

 

Copyright © 2024 ***Pepsi***
Powered by .NET 8.0 on Kubernetes