布局页面使用相对布局,包含一个编辑框、一个显示按钮和一个文本框,并在Styles.xml文件中指定样式。
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文本框内容
}
});
}
}