Android学习单击事件简单实例
2013-09-03 23:40
302 查看
一个简单的需求:
从输入框中输入内容,点击提交按钮后内容将显示在下方的显示区域!
![](http://img.blog.csdn.net/20130903234455578?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3loMDkxMDIwMTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
首先建立布局文件activity_main.xml
建立MainActivity.java文件
简单的需求完成,从而也了解了基本的单击事件的处理!
从输入框中输入内容,点击提交按钮后内容将显示在下方的显示区域!
首先建立布局文件activity_main.xml
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="36dp" android:ems="10" android:hint="@string/text_hint" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginLeft="34dp" android:layout_marginTop="18dp" android:text="@string/btn" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/button1" android:layout_marginTop="43dp" /> </RelativeLayout>
建立MainActivity.java文件
package com.example.onclickevent; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText editText=null; private Button button=null; private TextView textView=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.editText=(EditText)findViewById(R.id.editText1); this.textView=(TextView)findViewById(R.id.textView1); this.button=(Button)findViewById(R.id.button1); this.button.setOnClickListener(new showText()); } private class showText implements OnClickListener{ public void onClick(View v) { String info=MainActivity.this.editText.getText().toString(); MainActivity.this.textView.setText(info); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
简单的需求完成,从而也了解了基本的单击事件的处理!
相关文章推荐
- Android多媒体学习一:Android中Image的简单实例。
- Android学习 - 单击事件另两种实现方法
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences)
- Android学习之路---Button单击事件的实现方法
- Android学习之按钮单击事件及监听器的5实现方式
- Android多媒体学习一:Android中Image的简单实例。
- Android学习历程15-SurfaceView的简单用法(实例:幸运抽奖以及控制中奖物品)
- android学习(1) - Button单击事件的响应
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences) .
- 更简单的学习Android事件分发
- Android多媒体学习一:Android中Image的简单实例。
- Android中通过MotionEvent实现简单的单击事件
- 更简单的学习Android事件分发
- Android开发之button事件监听简单实例
- Android 开发实例教程 零基础学习 1小时入门 图解说明教程 化复杂为简单 配置好的android开发环境 带实例
- Android学习之SQLite数据库简单测试实例
- 【转载】更简单的学习Android事件分发
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences)
- 更简单的学习Android事件分发
- 学习笔记--android中surfaceview 简单用法实例:画板。