通过toast控件实现上述画面。
首先,创建一个Android项目,在其res文件夹下的layout文件夹内的activity_main.xml文件内做好UI页面的布局,内容如下:
<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"
android:background="@color/danzise" >
<TextView
android:id="@+id/tv_tixing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="114dp"
android:text="@string/tv_tixing" />
<EditText
android:id="@+id/et_print"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_tixing"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10"
android:drawableLeft="@drawable/ic_launcher"
android:background="@android:drawable/edit_text" />
<Button
android:id="@+id/tijiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_print"
android:layout_centerHorizontal="true"
android:text="@string/true_button" />
</RelativeLayout>
其中RelativeLayout中的android:background可以设置UI界面的背景颜色或背景图片:
背景颜色:android:background="@color/你所想设的颜色代码"
(最好在res文件夹的values文件夹下新建一个colors.xml的xml文件把颜色统一放置。如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="danzise">#CA8EFF</color>
</resources>
这是我建的colors的xml文件。
)
背景图片:android:background="@drawable/res文件夹下drawable文件夹内的图片名称"
对于显示在界面上的非用户输入文字,应在res文件夹的values文件夹内的string.xml文件内添加相关显示的内容,并给之命名。
如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloAndroid</string>
<string name="action_settings">Settings</string>
<string name="tv_tixing">请输入您的姓名</string>
<string name="true_button">提交</string>
</resources>
对EditText的属性小贴士:
对的显示框的系统封装属性的调用:android:background="@android:drawable/edit_text"(edit_text可根据需要选择系统封装的五个样式之一)
在文本框内添加图片:android:drawableLeft="@drawable/ic_launcher"语句可实现在文本框内添加图片,ic_launcher为系统图片或drawable内的图片。
* * * * * * * * * 对于UI界面中添加的各个控件,为方便之后在Java文件等内的应用,应取与其应用功能相关的名字做到见名之意。(及id值或name值)
当UI界面的大体布局设置好后,应编写src文件夹下的实现类MainActivity.java,如下:
package com.example.helloandroid;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@SuppressLint("ShowToast") public class MainActivity extends Activity {
protected static final Context MainActivity = null;
private EditText tixing;
private Button tijiao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tixing = (EditText) findViewById(R.id.et_print);
tijiao = (Button) findViewById(R.id.tijiao);
tijiao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tx = tixing.getText().toString();
System.out.println("hello" + tx);
Toast.makeText(MainActivity.this, "hello " + tx, Toast.LENGTH_LONG).show();
}
});
}
@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;
}
}
Button控件的点击事件后,监听方式有3种:
1.内部类
事件源与事件处理类绑定:
tijiao.setOnClickListener(new OnClick());
事件处理类:
class OnClick implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String tx = tixing.getText().toString();
System.out.println("hello" + tx);
Toast.makeText(MainActivity.this, "hello " + tx, Toast.LENGTH_LONG).show();
}
}
2.匿名内部类
直接调用button实体化的变量方法:
tijiao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tx = tixing.getText().toString();
System.out.println("hello" + tx);
Toast.makeText(MainActivity.this, "hello" + tx, Toast.LENGTH_LONG).show();
}
});
3.在局部文件中设置与事件相关的属性
在activity_main.xml文件中Button控件添加属性:
android:onClick="tiJiao"
在java文件中添加实体类:
public void tiJiao(View view){
String tx = tixing.getText().toString();
System.out.println("hello" + tx);
Toast.makeText(MainActivity.this, "hello" + tx, Toast.LENGTH_LONG).show();
}