EditText+Button组合自定义视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
  />
  <Button
    android:id="@+id/clearButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Clear"
  />
</LinearLayout>

package com.paad.views;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

/**
 * Listing 4-13: Constructing a compound View
 */
public class ClearableEditText extends LinearLayout {

  EditText editText;
  Button clearButton;

  public ClearableEditText(Context context) {
    super(context);

    // Inflate the view from the layout resource.
    String infService = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater li;
    li = (LayoutInflater)getContext().getSystemService(infService);
    li.inflate(R.layout.clearable_edit_text, this, true);

    // Get references to the child controls.
    editText = (EditText)findViewById(R.id.editText);
    clearButton = (Button)findViewById(R.id.clearButton);

    // Hook up the functionality
    hookupButton();
  }
    
  /**
   * Listing 4-14: Implementing the Clear Text Button
   */
  private void hookupButton() {
    clearButton.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        editText.setText("");
      }
    });
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义一个 Dialog,可以按照以下步骤进行操作: 1. 创建一个新的类来扩展 Dialog 类。在这个新类,你可以定义你自己的布局和逻辑。 2. 在 onCreate() 方法,加载布局并通过 findViewById() 方法获取视图元素的引用。 3. 在 show() 方法,设置 Dialog 的大小和样式,然后将布局设置为 Dialog 的内容视图。 4. 在布局添加必要的视图元素,如 TextView、EditTextButton 等。 5. 在适当的位置为这些视图元素添加响应事件的侦听器。 以下是一个简单的示例代码: ``` class MyDialog(context: Context) : Dialog(context) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.my_dialog_layout) // 获取视图元素 val titleTextView = findViewById<TextView>(R.id.titleTextView) val cancelButton = findViewById<Button>(R.id.cancelButton) // 为按钮添加点击事件侦听器 cancelButton.setOnClickListener { dismiss() } } override fun show() { super.show() // 设置 Dialog 的大小和样式 window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT) window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) } } ``` 在这个示例,我们创建了一个名为 MyDialog 的新类,它扩展了 Dialog 类。在 onCreate() 方法,我们加载了自定义布局,并使用 findViewById() 方法获取了 TextView 和 Button 的引用。在适当的位置,我们为取消按钮添加了一个点击事件侦听器。在 show() 方法,我们设置了 Dialog 的大小和样式,并将布局设置为 Dialog 的内容视图。 最后,我们可以在需要的地方创建并显示这个 Dialog: ``` val dialog = MyDialog(context) dialog.show() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值