自定义View中的控件实现页面的跳转
来源:互联网 发布:张公子知乎 编辑:程序博客网 时间:2024/05/17 22:31
1:自定义的view中需要获取Context,在构造方法中定义即可,然后初始化界 面可控件!(这里调用一个button按钮)
public class Views extends RelativeLayout{ private Button btu; private Context context; public Views(final Context context, AttributeSet attrs) { super(context, attrs); this.context = context; LayoutInflater.from(context).inflate(R.layout.aaa, this); btu = (Button) findViewById(R.id.button1); btu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub context.startActivity(new Intent(getContext(), MainActivity.class)); } }); }}
2:一般情况下设置一个Context为全局变量,方便调用,不过调用较少的话,可以不用设置全局变量,直接调用构造方法中的即可!(上面已经实现)
3:在自己的activity中初始化自定义的类,然后就获得button的点击效果
public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); Views view = new Views(this, null); view = (Views) findViewById(R.id.button); }}
补充4:Activityde的布局为:(中间是引用自定义的Views)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".OtherActivity" > <cn.com.example.donghua.Views android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"> </cn.com.example.donghua.Views></RelativeLayout>
0 0
- 自定义View中的控件实现页面的跳转
- [自定义VIEW]仿闪屏页面的倒计时控件
- 自定义项目中的404页面,并且实现指定时间内跳转到一个指定的页面
- 自定义控件01---简单view的实现
- 如何实现点击UITableViewCell中的控件,能调用到所在的ViewController对象进行页面跳转
- 自定义View圆形进度条带跳转页面
- View页面间的跳转
- 【自定义控件】自定义View实现TextView的简单功能
- 框架中的页面实现跳转
- 跳转页面的实现
- NSIS自定义页面的跳转
- 自定义的view中控件的点击事件实现
- 自定义view之超级课程表页面的实现
- Android自定义view之环形等待控件的实现
- android控件架构和自定义View的实现
- android继承View实现复杂的自定义控件(1)
- Android之自定义View实现随手势滑动的控件
- Android 自定义View 实现方向盘控件的绘制
- 机器学习笔记(三)牛顿法和梯度下降法
- 数组的几种复制方法
- NKOI 3079 遗传密码
- 工具那些事--Sublime Text GBK支持
- “打开或关闭Windows功能”窗口之后就是空白的
- 自定义View中的控件实现页面的跳转
- 冒泡法
- C语言中的atan和atan2
- Kernel启动流程源码解析 4 start_kernel 上
- JDO快速入门
- java方法的重载
- C语言入门
- 如何获得当前类的字段,方法参数泛型的类型
- 最简单的 NSIS 函数返回值的返回与接收实例