一、自定义控件

  MotionEvent.ACTION_UP:抬起
  MotionEvent.ACTION_DOWN: 按下
  MotionEvent.ACTION_POINTER_UP:
  MotionEvent.ACTION_POINTER_DOWN:

先产生一个ACTION_DOWN事件,代表用户的第一个手指接触到了屏幕。

再产生一个ACTION_POINTER_DOWN事件,代表用户的第二个手指接触到了屏幕。

  案例:

  public class MyButton extends android.support.v7.widget.AppCompatButton {

//三个构造方法
public MyButton(Context context) {
super(context);
}

public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

//重写onTouchEvent触碰时间的方法
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
setScaleX(0.5f);
setScaleY(0.5f);
break;
case MotionEvent.ACTION_UP:
setScaleX(1.0f);
setScaleY(1.0f);

break;
case MotionEvent.ACTION_POINTER_UP:
// setScaleX(1.0f);
// setScaleY(1.0f);
// break;
case MotionEvent.ACTION_POINTER_DOWN:
//
// break;
}
return super.onTouchEvent(event);
}
}

二、自定义控件在布局(Linearlayout)

  

  1.定义好布局文件来供布局(LinearLayout、FrameLayout)来继承解析

  2.继承布局(LinearLayout、FrameLayout)来解析xml文件,里面可以写一下它的构造方法

  3.在主布局xml文件中去加载写好的布局,像和加载控件一样去加载

  4.在MainActivity中findViewById来绑定布局

  1.定义好布局文件来供Fragment来解析  (被引用时候不需要ID)

    R.layout.mylogin.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="wrap_content">

<TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="账号" />

<EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:inputType="textPassword" />
    </LinearLayout>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="密码" />

<EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:inputType="textPassword" />
    </LinearLayout>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/buttonLoin"
        />
</LinearLayout>

2.2.继承布局(LinearLayout、FrameLayout)来解析xml文件,里面可以写一下它的构造方法

  public class MyLogin extends LinearLayout {
    private Button buttonLogin;
    private EditText editPassword, editLogin;

public MyLogin(Context context) {
        super(context);
        init(context);
        Log.i("MyLogin", "MyLogin1: ");
    }
    
    public MyLogin(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
        Log.i("MyLogin", "MyLogin2: ");
    }

public MyLogin(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
        Log.i("MyLogin", "MyLogin3: ");
    }

public MyLogin(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

public void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.mylogin, MyLogin.this, true);
        buttonLogin = view.findViewById(R.id.buttonLoin);
        editLogin = view.findViewById(R.id.editText);
        editPassword = view.findViewById(R.id.editText1);
    }

  //重写点击时间的方法
    public void setOnClickListener(View.OnClickListener listener) {
        buttonLogin.setOnClickListener(listener);
    }

}

3.在主布局xml文件中去加载写好的布局,像和加载控件一样去加载(引用时候需要ID)

  <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

<com.example.mi.custombutton.MyLogin            --------------------------->这里可以通过找到findViewById找到对应的View------------>linearLayout = findViewById(R.id.login);
                          button = linearLayout.findViewById(R.id.buttonLoin);     //因为linearLayout是View的视图,所以可以在一次来findViewById来找到其他的控件
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

4.在MainActivity中findViewById来绑定布局

  public class  MainActivity extends AppCompatActivity {
    private LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout = findViewById(R.id.login);
  

  //button = linearLayout.findViewById(R.id.buttonLoin);     //因为linearLayout是View的视图,所以可以在一次来findViewById来找到其他的控件

                              有了这一步,就不需要再重写这个控件的方法了

  //已经重写点击事件的代码
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this  , "你成功了" , Toast.LENGTH_SHORT).show();
            }
        });
    }
}

三、自定义布局文件(FrameLayout)

  1.定义好FrameLayout的布局                                        //这里不需要定义布局文件的ID,只是为了给下面的地方作为引用

   <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher" />

<TextView
android:id="@+id/text_exercise"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我再写测试代码"
android:gravity="center"
android:textColor="#11f"
/>
</FrameLayout>

2.自定义布局文件

  public class MyFrame extends FrameLayout {
private ImageView imageView;
private TextView textView;
public MyFrame (Context context) {
super(context);
init(context);
}
public MyFrame(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyFrame(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public MyFrame(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
public void init(Context context){
View view = LayoutInflater.from(context).inflate(R.layout.myframe, MyFrame.this, true);
imageView = findViewById(R.id.icon);
textView = findViewById(R.id.text_exercise);
}

public void setImageResource(int resource){
imageView.setImageResource(resource);
}

public void setText(String string){
textView.setText(string);
}
}

3.加载自定义布局文件

<?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"
xmlns:tools="http://schemas.android.com/tools"
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="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <com.example.mi.custombutton.MyFrame
android:id="@+id/myFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" /> //这里一定定义ID,为了后面便于查找
</LinearLayout> 4.代码加载并且编写
  public class MainActivity extends AppCompatActivity {
private MyFrame frameLayout;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.login);
frameLayout = findViewById(R.id.myFrame);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this , "你成功了" , Toast.LENGTH_SHORT).show();
}
});
         //通过对两个函数重写的方式来调用它的子控件
frameLayout.setImageResource(R.drawable.ic_launcher_foreground);
frameLayout.setText("我可以改变"); }
}

     

  

  

    

  

  

  

自定义控件和View的更多相关文章

  1. android自定义控件(四) View中的方法

    onFinishInflate() 当View中所有的子控件 均被映射成xml后触发 onMeasure(int, int) 确定所有子元素的大小 onLayout(boolean, int, int ...

  2. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  3. Android自定义控件1--自定义控件介绍

    Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...

  4. View相关知识学习总结

    (一)LayoutInflater原理分析 LayoutInflater主要用于加载布局.通常情况下,加载布局的任务都是在Activity中调用setContentView()方法来完成的,该方法内部 ...

  5. Android View和ViewGroup

    View和ViewGroup Android的UI界面都是由View和ViewGroup及其派生类组合而成的. 其中,View是所有UI组件的基类,而 ViewGroup是容纳这些组件的容器,其本身也 ...

  6. View与ViewGroup有什么区别?

    百度知道:http://zhidao.baidu.com/link?url=B5MFOzDlww8soYqr5CL5FldH4sXD6eumS1XTRn8XEh8gu4mKjQdPkJSLIBt7u_ ...

  7. 手机安全卫士——在设置中心 自定义view和自定义属性

    自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载   ...

  8. 031 Android 自定义控件

    1.自定义控件的优点 Android自身带的控件不能满足需求, 需要根据自己的需求定义控件. 2.自定义控件的分类: (1)组合已有的控件实现 (2)继承已有的控件实现(扩展已有的功能) (3)完全自 ...

  9. Android 中View的工作原理

    Android中的View在Android的知识体系中扮演着重要的角色.简单来说,View就是Android在视觉的体现.我们所展现的页面就是Android提供的GUI库中控件的组合.但是当要求不能满 ...

随机推荐

  1. Xcode.Subproject.And.Framework

    1. Easy Xcode Static Library Subprojects and Submodules http://www.blog.montgomerie.net/easy-xcode-s ...

  2. 欲哭无泪的p-value = 0.051 | 做几次重复能得到较低的p-value

    欲哭无泪的p-value = 0.051 | 做几次重复能得到较低的p-value 已有 1469 次阅读 2017-12-15 14:12 |个人分类:RNA-seq|系统分类:科普集锦|关键词:R ...

  3. POJ1180 Batch Scheduling -斜率优化DP

    题解 将费用提前计算可以得到状态转移方程: $F_i = \min(F_j + sumT_i * (sumC_i - sumC_j) + S \times (sumC_N - sumC_j)$ 把方程 ...

  4. 理解OAuth 2.0 (摘自阮一峰网络日志)

    OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...

  5. Jmeter通过BeanShell Sampler获取Jmeter的Bin路径,并存入变量供后面的脚本调用

    Jmeter的Bin路径是其运行路径,当把自动化测试的脚本放在Bin目录下时,为了将存储CSV的数据文件以及脚本的路径都设置成相对路径,我们需要获取到Jmeter的运行路径: 通过BeanShell ...

  6. jqgrid单元格合并

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  7. Highcharts做柱状图怎样样每个柱子都是不同的颜色显示

    series: [{ data: [{'color':'#F6BD0F','y':11}, {'color':'#AFD8F8','y':12}, {'color':'#8BBA00','y':13} ...

  8. list集合如何对里面的元素进行排序

    Collections 是集合的公共类,提供各种工具,其中提供了排序方法. Collections.sort(),方法两个参数,1,要排序的集合,2.排序方式 下面是匿名内部类,实现了排序借口,你也可 ...

  9. 使用java做paypal开发时购买东西支付不成功的原因

    使用java做paypal开发时购买东西支付不成功的原因 没有设置网站习惯设定,登陆自己的paypal账户,在网站习惯设定上填写回调的url路径,这样就可以 支付成功了并且异步修改订单的状态. 支付成 ...

  10. 2018.08.17 洛谷P3110 [USACO14DEC]驮运(最短路)

    传送门 一道sb最短路,从两个起点和终点跑一边最短路之后直接枚举两人的汇合点求最小值就行了. 代码: #include<bits/stdc++.h> #define N 40005 #de ...