zoukankan      html  css  js  c++  java
  • 自定义控件

    点击Button,EditText里的字会清空

    MainActivity:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical" >
     5 
     6     <com.example.canvastest.MyView
     7         android:layout_width="match_parent"
     8         android:layout_height="0dp"
     9         android:layout_weight="1" />
    10 
    11 </LinearLayout>
    1 public class MainActivity extends Activity {
    2     public static final String TAG = "MainActivity";
    3 
    4     @Override
    5     protected void onCreate(Bundle savedInstanceState) {
    6         super.onCreate(savedInstanceState);
    7         setContentView(R.layout.activity_main);
    8     }
    9 }

    MyView:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <EditText
     8         android:id="@+id/edt"
     9         android:layout_width="match_parent"
    10         android:layout_height="wrap_content" />
    11 
    12     <Button
    13         android:id="@+id/btn"
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:text="clear" />
    17 
    18 </LinearLayout>
     1 public class MyView extends LinearLayout implements View.OnClickListener {
     2     private EditText edt;
     3     private Button btn;
     4 
     5     public MyView(Context context, AttributeSet attrs) {
     6         super(context, attrs);
     7 
     8         LayoutInflater inflater = (LayoutInflater) context
     9                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    10         View view = inflater.inflate(R.layout.myview, this);
    11 
    12         edt = (EditText) view.findViewById(R.id.edt);
    13 
    14         btn = (Button) view.findViewById(R.id.btn);
    15         btn.setOnClickListener(this);
    16     }
    17 
    18     @Override
    19     public void onDraw(Canvas canvas) {
    20         super.onDraw(canvas);
    21     }
    22 
    23     @Override
    24     public void onClick(View v) {
    25         switch (v.getId()) {
    26         case R.id.btn:
    27             edt.setText("");
    28             break;
    29         }
    30     }
    31 }

  • 相关阅读:
    python之约束、加密及logging模块
    python之反射机制与callattr()、issubclass()、isinstance、type()相关
    python之面向对象初识
    python函数名的应用、闭包和迭代器
    python(动态传参、命名空间、函数嵌套、global和nonlocal关键字)
    python中函数的定义、返回值以及参数的简要介绍
    python文件操作
    python中set(集合),深浅拷贝以及一些补充知识点
    python中is与==的区别,编码和解码
    python数据类型:dict(字典)
  • 原文地址:https://www.cnblogs.com/songsiyao/p/3610800.html
Copyright © 2011-2022 走看看