通过实例学安卓开发
序
对上两个实例进行整合练习,Let’s do it!
实例
题目
动态加载布局的使用——弹窗效果。
程序结果展示界面
涉及到的知识点
inflate()直观意义是填充,用于实现视图的动态载入,其主要代码如下:
LayoutInflater inflater = this.getLayoutInflater(); //获取LayoutInflater对象
View layout = inflater.inflate(R.layout.login_view,null);//加载布局文件
实现过程
- 新建名为DynamicLayout的Android应用工程
- 新建名为login_view.xml的布局文件
- 修改主页的布局文件:activity_main.xml
- 编写工程的主Activity
源码
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态加载布局文件,实现弹窗效果"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/btn" android:text="登录" android:textSize="22sp"/>
</LinearLayout>
login_view.xml
<?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"
>
<LinearLayout android:layout_width="match_parent" android:layout_height="50dp"
android:orientation="horizontal">
<TextView android:layout_width="90dp" android:layout_height="wrap_content"
android:text="用户名:" android:textSize="20sp"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/user"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="50dp"
android:orientation="horizontal">
<TextView android:layout_width="90dp" android:layout_height="wrap_content"
android:text="密码:" android:textSize="20sp"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/password"
android:password="true"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.dynamiclayout;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
View layout;
EditText et1 ;
EditText et2 ;
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater =MainActivity.this.getLayoutInflater();
layout = inflater.inflate(R.layout.login_view,null);
builder.setTitle("用户登录");
builder.setView(layout);
builder.setPositiveButton("登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
et1 = layout.findViewById(R.id.user);
et2 = layout.findViewById(R.id.password);
Toast.makeText(MainActivity.this,"欢迎用户"+et1.getText().toString()+",您的密码是"+et2.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn =(Button) MainActivity.this.findViewById(R.id.btn);
btn.setOnClickListener(listener);
}
}
总结
组件最好都定义为全局变量,否则容易报空指针异常。