一手遮天 Android - view(自定义): 自定义组合控件
一手遮天 Android - view(自定义): 自定义组合控件
示例如下:
/view/custom/CustomView3Demo.java
/**
* 演示如何自定义组合控件,自定义的组合控件参见 view/custom/CustomView3.java(实现了一个简易的登录框)
*/
package com.webabcd.androiddemo.view.custom;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.webabcd.androiddemo.R;
public class CustomView3Demo extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_custom_customview3demo);
CustomView3 customView3 = findViewById(R.id.customView3);
customView3.setOnLoginListener(new CustomView3.OnLoginListener() {
@Override
public void login(View v, String username, String password) {
// 用户点击登录按钮后,可以拿到用户输入的用户名和密码
Toast.makeText(CustomView3Demo.this, String.format("username:%s, password:%s", username, password), Toast.LENGTH_SHORT).show();
}
});
}
}
/layout/activity_view_custom_customview3demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--
CustomView3 - 自定义组合控件(实现了一个简易的登录框)
-->
<com.webabcd.androiddemo.view.custom.CustomView3
android:id="@+id/customView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:usernameLabel="用户名"
app:passwordLabel="密 码" />
</LinearLayout>