Android开发之onClick事件的实现

算是从2015年开始学习android开发,目前把onClick的事件实现写下来,记录下,以备参考。

实现button的点击功能,让textView显示一行文字,最简单的onClick事件。

直接贴代码:

    public void onClick_method(View view){
        String contextString="点击了这个按钮,实现了点击功能";
        TextView textView=(TextView) findViewById(R.id.testTV);
        textView.setText(contextString);       
    }

布局文件:

    <TextView
        android:id="@+id/testTV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hello_world"/>
    <Button 
        android:onClick="onClick_method"
        android:id="@+id/button"
        android:layout_below="@id/testTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"
        />

请看下面的实现效果图:

点击前:

点击后:

看到还有其他的方法, 但是我感觉这种是最简单明了的,以后开发使用这种就可以了。

posted @ 2015-02-11 20:40  熠然  阅读(932)  评论(0编辑  收藏  举报