Android(安卓)Studio入门小例子
16lz
2021-12-04
目录
- Android Studio入门小例子
- activity_main.xml
- MainActivity
- 效果图
Android Studio入门小例子
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity" android:weightSum="1"> <EditText android:id="@+id/myed" android:layout_width="188dp" android:layout_height="wrap_content" android:text="请输入您的姓名"/> <Button android:id="@+id/mybut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示输入信息"/> <TextView android:id="@+id/mytext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入的信息是:"/>LinearLayout>
MainActivity
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends AppCompatActivity { private TextView showView = null; private EditText edit = null; private Button but = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.but = (Button)super.findViewById(R.id.mybut); this.showView = (TextView)super.findViewById(R.id.mytext); this.edit = (EditText)super.findViewById(R.id.myed); this.edit.setOnClickListener(new OnClickListenerImpl()); this.edit.setOnFocusChangeListener(new OnFocusChangeListenerImpl()); but.setOnClickListener(new ShowLIstener()); } private class OnFocusChangeListenerImpl implements View.OnFocusChangeListener{ @Override public void onFocusChange(View v, boolean hasFocus) { if(v.getId()==MainActivity.this.edit.getId()){ if(hasFocus){ MainActivity.this.showView.setText("文本输入 组件获得焦点。"); }else{ if(MainActivity.this.edit.getText().length()>0){ }else{ MainActivity.this.showView.setText("文本输入组件失去焦点,输入内容不能为空"); } } } } } private class OnClickListenerImpl implements View.OnClickListener{ public void onClick(View v) { MainActivity.this.edit.setText(""); } } private class ShowLIstener implements View.OnClickListener{ public void onClick(View view){ String info = edit.getText().toString(); showView.setText("输入的内容是:"+info); } }}
效果图
效果图
更多相关文章
- Android(安卓)EditText 限制文本框输入的长度和提示信息
- UI组件
- android 组件
- Android概述
- Android-AbsoluteLayout(绝对布局)
- Android概述
- Android概述
- Android概述
- Android的UI组件之TextView、EditText