<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">注册界面的设计效果如下: </span>
今天主要讲的Toast的用法:
1.默认Toast
Toast.makeText(context, text, duration).show();//context为使用的上下文,text表示要显示的字符串,duration表示显示的时间,值为:Toast.LENGTH_LONG,Toast.LENGTH_SHORT
切记在引用的时候不要忘记.show。
2.自定义Toast显示位置
Toast toast = Toast.makeText(this, "这是自定义位置的toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);//设置toast显示的位置
toast.show();
3.带图标的Toast
Toast toast = Toast.makeText(this, "这是带图标的toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);//设置toast显示的位置
LinearLayout toastview = (LinearLayout)toast.getView();//获取Toast的view对象
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.ic_launcher);
toastview.addView(image);
toast.setView(toastview);//将view显示在Toast上
toast.show();
好了,下面介绍界面的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入您的用户名:"/>
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint=" "/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择您的性别:"/>
<RadioGroup
android:id="@+id/radioGroup1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/maleButton"
android:text="男"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/femaleButton"
android:text="女"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择您的爱好:"/>
<CheckBox
android:id="@+id/singBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="唱歌"/>
<CheckBox
android:id="@+id/danceBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳舞"/>
<CheckBox
android:id="@+id/drawBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="画画"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注 册"/>
</LinearLayout>
MainActivity中的代码如下:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private RadioGroup genderGroup;// 定义单选按钮组
private RadioButton maleBtn, femaleBtn;
private CheckBox singCheckBox, danceCheckBox, drawCheckBox;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
genderGroup = (RadioGroup) findViewById(R.id.radioGroup1);
maleBtn = (RadioButton) findViewById(R.id.maleButton);
femaleBtn = (RadioButton) findViewById(R.id.femaleButton);
singCheckBox = (CheckBox) findViewById(R.id.singBox);
danceCheckBox = (CheckBox) findViewById(R.id.danceBox);
drawCheckBox = (CheckBox) findViewById(R.id.drawBox);
// 对RadioGroup注册事件监听器
genderGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String sexString = "";// 定义显示性别的字符串变量
if (maleBtn.getId() == checkedId) {// 根据对checkedId判断用户的选项
sexString = sexString + maleBtn.getText();// 调用了getText()方法获得按钮文本
} else if (femaleBtn.getId() == checkedId) {
sexString = sexString + femaleBtn.getText();
}
Toast.makeText(MainActivity.this, "您选择的性别是:" + sexString,
Toast.LENGTH_LONG).show();
}
});
// 下面分别对每个CheckBox实现事件监听
singCheckBox.setOnCheckedChangeListener(new Listener());
danceCheckBox.setOnCheckedChangeListener(new Listener());
drawCheckBox.setOnCheckedChangeListener(new Listener());
}
class Listener implements
android.widget.CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
String str = "";// 定义显示爱好的字符串变量
if (singCheckBox.isChecked()) {// 调用isChecked()方法,判断哪个选项被选中
str = str + singCheckBox.getText();
}
if (danceCheckBox.isChecked()) {
str = str + danceCheckBox.getText();
}
if (drawCheckBox.isChecked()) {
str = str + drawCheckBox.getText();
}
Toast.makeText(MainActivity.this, "您选择的爱好是:" + str,
Toast.LENGTH_LONG).show();
}
}
}