zoukankan      html  css  js  c++  java
  • android 事件绑定

    layout布局设计了页面,如何绑定事件,与用户进行交互需要在Activity中进行处理。

    下面的layout,有两个按钮。

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/login" />
    
            <Button
                android:id="@+id/register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/register" />
        </LinearLayout>

    绑定事件在ActivityonCreate方法中实现。

    Button register;
    
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    
    register = (Button) findViewById(R.id.register);
            register.setOnClickListener(new RegisterOnclick());
    }
    
    private class RegisterOnclick implements View.OnClickListener {
            public void onClick(View v) {
    }
    }

    另外还有一种方式需要在Activity继承OnClickListener

    public class ActivityInstace extends Activity  implements OnClickListener {
    Button register;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            register = (Button) findViewById(R.id.register);
    
            register.setOnClickListener(this);
    }
    @Override
        public void onClick(View v) {
    }
    }
  • 相关阅读:
    python-socket1
    python-网络编程
    linux-常用指令3
    linux-vim/编辑器
    linux-常用指令2
    linux-常用指令1
    MySQL 练习题
    33 python 并发编程之IO模型
    32 python 并发编程之协程
    31 python下实现并发编程
  • 原文地址:https://www.cnblogs.com/lucika/p/5692015.html
Copyright © 2011-2022 走看看