Android学习——简单的Button按钮
2013-04-07 16:20
232 查看
布局页面使用相对布局,包含一个编辑框、一个显示按钮和一个文本框,并在Styles.xml文件中指定样式。
activity_text.xml:
styles.xml:
在java代码中处理按钮的单击事件,单击显示按钮在文本框中显示编辑框中输入的内容。
TextActivity.java:
activity_text.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/machine_div" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dip" android:padding="5dip" > <EditText android:id="@+id/editInput" style="@style/EditText_Style" android:layout_width="200dp" android:layout_height="wrap_content" android:singleLine="true" /> <Button android:id="@+id/btnOK" style="@style/Button_Style" android:layout_toRightOf="@+id/editInput" android:text="显示" /> <!-- 下画线 --> <View android:id="@+id/work_Line" android:layout_width="fill_parent" android:layout_height="1dp" android:layout_below="@+id/editInput" android:background="#F5B50D" /> <TextView android:id="@+id/txtInfo" style="@style/EditText_Style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/work_Line" /> </RelativeLayout>
styles.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> <!-- TextView Style --> <style name="TextView_Style"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textSize">20sp</item> <item name="android:autoLink">all</item> </style> <!-- Button Style --> <style name="Button_Style"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textSize">22sp</item> <item name="android:textStyle">bold</item> </style> <!-- EditText Style --> <style name="EditText_Style"> <item name="android:textSize">18sp</item> <item name="android:selectAllOnFocus">true</item> </style> </resources>
在java代码中处理按钮的单击事件,单击显示按钮在文本框中显示编辑框中输入的内容。
TextActivity.java:
package com.test.myandroidtest; 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 TextActivity extends Activity { private Button btnOK = null; private EditText editInput = null; private TextView txt = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_text); // 指定布局文件 btnOK = (Button)findViewById(R.id.btnOK); // 获取布局界面控件 btnOK.setOnClickListener(new OnClickListener() { // 为按钮设置监听器并重写onClick事件 @Override public void onClick(View v) { editInput = (EditText)findViewById(R.id.editInput); txt = (TextView)findViewById(R.id.txtInfo); txt.setText(editInput.getText()); // 设置TextView文本框内容 } }); } }
相关文章推荐
- 一只大二狗的Android历程--简单自定义按钮样式(Custom Button Style)
- Android学习起步 - Button按钮及事件处理
- 2.4简单的按钮----Android Button
- (从零开始学Android)编辑框EditText与按钮Button学习心得
- Android学习路之Button(按钮)与ImageButton(图像按钮)
- Android学习路之开关按钮ToggleButton和开关Switch
- android简单demo学习系例之按钮
- Android(java)学习笔记113:Android编写代码调用Vibrator震动功能(Bug:按下按钮button始终没有震动)
- android中关于美化Button按钮的一些简单操作(其他控件也类似)
- Android开发基础之Button按钮的简单用法
- android 学习 ---控件按钮,Button 与ImageButton,
- android 客户端学习-给button按钮设置圆角
- Android学习 12-> 按钮Button
- 学习Android,最简单的按钮提示文本信息
- Android基础学习 - 简单控件,EditText 、 Button :
- android客户端学习-button按钮边框及圆角
- Android开发学习笔记:圆角的Button
- [Android开发] 自定义View之重写View非常简单实现开关按钮SwitchView
- Android的ToggleButton和Switch以及AnalogColok和DigitalColok的用法-android学习之旅(二十)
- APIDemo学习笔记——Android上几种简单的Animation使用方法(三)——3D Transition