UI就是用户界面User Interface
什么是布局
——定义UI的可视化结构
——通过布局参数(LayoutParams)定义子元素的尺寸、位置
——布局是不可见的容器(ViewGroup)
LinearLayout布局的应用
RelativeLayout布局的应用
TextView控件
TextView从父类View派生而来
EditText控件
EditText控件是从TextView控件继承下来
Button控件的应用
Button空间是TextView控件的派生类,同时通过TextView继承了View的多种属性
package com.example.button;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//定义提供给按钮调用的一个方法
public void BtnBmiCalc_Click(View v){
//读取身高体重文本框的数值
EditText editHeight = (EditText)findViewById(R.id.editHeight);//控件的id号,R.id作为前缀
EditText editWeight = (EditText)findViewById(R.id.editWeight);
TextView txtResult = (TextView)findViewById(R.id.txtResult);
Double height = Double.parseDouble(editHeight.getText().toString());//将字符串转为浮点数
Double weight = Double.parseDouble(editWeight.getText().toString());
Double bmi = weight / (height*height);
//setText就是输出响应文字
if(bmi < 18.5){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过轻");
}
else if(bmi <= 23.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:正常");
}
else if(bmi <= 27){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过重");
}
else if(bmi <= 32){
txtResult.setText("BMI:" + bmi.toString() + " 体型:肥胖");
}
else{
txtResult.setText("BMI:" + bmi.toString() + " 体型:非常肥胖");
}
}
}
法2
package com.example.button;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnBmiCalc = (Button)findViewById(R.id.btnBmiCalc);
btnBmiCalc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//读取身高体重文本框的数值
EditText editHeight = (EditText)findViewById(R.id.editHeight);//控件的id号,R.id作为前缀
EditText editWeight = (EditText)findViewById(R.id.editWeight);
TextView txtResult = (TextView)findViewById(R.id.txtResult);
Double height = Double.parseDouble(editHeight.getText().toString());//将字符串转为浮点数
Double weight = Double.parseDouble(editWeight.getText().toString());
Double bmi = weight / (height*height);
//setText就是输出响应文字
if(bmi < 18.5){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过轻");
}
else if(bmi <= 23.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:正常");
}
else if(bmi <= 27){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过重");
}
else if(bmi <= 32){
txtResult.setText("BMI:" + bmi.toString() + " 体型:肥胖");
}
else{
txtResult.setText("BMI:" + bmi.toString() + " 体型:非常肥胖");
}
}
});
}
}
RadioButton控件的应用
RadioButton的继承关系图
wrap content 是根据内容的高度自动适配
package com.example.button;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnBmiCalc = (Button)findViewById(R.id.btnBmiCalc);
btnBmiCalc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//读取身高体重文本框的数值
EditText editHeight = (EditText)findViewById(R.id.editHeight);//控件的id号,R.id作为前缀
EditText editWeight = (EditText)findViewById(R.id.editWeight);
TextView txtResult = (TextView)findViewById(R.id.txtResult);
Double height = Double.parseDouble(editHeight.getText().toString());//将字符串转为浮点数
Double weight = Double.parseDouble(editWeight.getText().toString());
Double bmi = weight / (height*height);
RadioButton rbtnStdWho = (RadioButton)findViewById(R.id.rbtnStdWho);
RadioButton rbtnStdAisa = (RadioButton)findViewById(R.id.rbtnStdAisa);
RadioButton rbtnStdChn = (RadioButton)findViewById(R.id.rbtnStdChn);
if(rbtnStdWho.isChecked()) {//判断是否被选中
//setText就是输出响应文字
if(bmi < 18.5){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过轻");
}
else if(bmi <= 24.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:正常");
}
else if(bmi <= 29.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过重");
}
else if(bmi <= 34.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:肥胖");
}
else{
txtResult.setText("BMI:" + bmi.toString() + " 体型:非常肥胖");
}
}
else if(rbtnStdAisa.isChecked()){
//setText就是输出响应文字
if(bmi < 18.5){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过轻");
}
else if(bmi <= 22.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:正常");
}
else if(bmi <= 24.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过重");
}
else if(bmi <= 29.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:肥胖");
}
else{
txtResult.setText("BMI:" + bmi.toString() + " 体型:非常肥胖");
}
}
else if(rbtnStdChn.isChecked()){
//setText就是输出响应文字
if(bmi < 18.5){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过轻");
}
else if(bmi <= 23.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:正常");
}
else if(bmi <= 27.9){
txtResult.setText("BMI:" + bmi.toString() + " 体型:过重");
}
else{
txtResult.setText("BMI:" + bmi.toString() + " 体型:非常肥胖");
}
}
else
{
txtResult.setText("请选择BMI标准");
}
}
});
}
}
CheckBox控件的应用
综合小案例
package com.example.littleprogram;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton loginByUserName = (RadioButton)findViewById(R.id.loginByUserName);
RadioButton loginByPwd = (RadioButton)findViewById(R.id.loginByPwd);
String DbUser,DbPwd;
if(loginByUserName.isChecked()){
DbUser = "zhangsan";
DbPwd = "123456";
}
else{
DbUser = "zhangsan@sample.com";
DbPwd = "123456";
}
EditText txtUserName = (EditText)findViewById(R.id.txtUserName);
EditText txtPwd = (EditText)findViewById(R.id.txtPwd);
TextView txtResult = (TextView)findViewById(R.id.txtResult);
if(txtUserName.getText().toString().equals(DbUser))
{
if(txtPwd.getText().toString().equals(DbPwd)){
txtResult.setText("登录成功");
}
else{
//密码不存在
txtResult.setText("密码有误");
}
}
else{
//用户名不存在
txtResult.setText("用户名不存在");
}
}
});
}
}
FrameLayout布局
Toast控件的应用
Toast是消息提示控件
方法一:默认纯文字提示
方法二:自定义提示位置
package com.example.littleprogram;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton loginByUserName = (RadioButton)findViewById(R.id.loginByUserName);
RadioButton loginByPwd = (RadioButton)findViewById(R.id.loginByPwd);
String DbUser,DbPwd;
if(loginByUserName.isChecked()){
DbUser = "zhangsan";
DbPwd = "123456";
}
else{
DbUser = "zhangsan@sample.com";
DbPwd = "123456";
}
EditText txtUserName = (EditText)findViewById(R.id.txtUserName);
EditText txtPwd = (EditText)findViewById(R.id.txtPwd);
TextView txtResult = (TextView)findViewById(R.id.txtResult);
if(txtUserName.getText().toString().equals(DbUser))
{
if(txtPwd.getText().toString().equals(DbPwd)){
//txtResult.setText("登录成功");
Toast toast = Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG);
//LENGTH_SHORT是弹出消息框的显示时间,默认是短的
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
else{
//密码不存在
//txtResult.setText("密码有误");
Toast.makeText(MainActivity.this, "密码有误", Toast.LENGTH_LONG).show();
}
}
else{
//用户名不存在
//txtResult.setText("用户名不存在");
Toast.makeText(MainActivity.this, "用户名不存在", Toast.LENGTH_LONG).show();
}
}
});
}
}
方法三:自定义带图片信息提示
package com.example.littleprogram;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton loginByUserName = (RadioButton)findViewById(R.id.loginByUserName);
RadioButton loginByPwd = (RadioButton)findViewById(R.id.loginByPwd);
String DbUser,DbPwd;
if(loginByUserName.isChecked()){
DbUser = "zhangsan";
DbPwd = "123456";
}
else{
DbUser = "zhangsan@sample.com";
DbPwd = "123456";
}
EditText txtUserName = (EditText)findViewById(R.id.txtUserName);
EditText txtPwd = (EditText)findViewById(R.id.txtPwd);
TextView txtResult = (TextView)findViewById(R.id.txtResult);
if(txtUserName.getText().toString().equals(DbUser))
{
if(txtPwd.getText().toString().equals(DbPwd)){
//txtResult.setText("登录成功");
ToastShow("登录成功");
}
else{
//密码不存在
//txtResult.setText("密码有误");
//Toast.makeText(MainActivity.this, "密码有误", Toast.LENGTH_LONG).show();
ToastShow("密码有误");
}
}
else{
//用户名不存在
//txtResult.setText("用户名不存在");
//Toast.makeText(MainActivity.this, "用户名不存在", Toast.LENGTH_LONG).show();
ToastShow("用户名不存在");
}
}
});
}
void ToastShow(String Content){
Toast toast = Toast.makeText(MainActivity.this, Content, Toast.LENGTH_LONG);
//LENGTH_SHORT是弹出消息框的显示时间,默认是短的
toast.setGravity(Gravity.CENTER,0,0);
LinearLayout layout = (LinearLayout)toast.getView();
ImageView img = new ImageView(getApplicationContext());
img.setImageResource(R.drawable.ic_launcher);
layout.addView(img,0);
toast.show();
}
}
方法四:自定义布局的信息提示
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。
新建一个layout来自定义我们想让它显示的格式
使我们自定义的形式
package com.example.littleprogram;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton loginByUserName = (RadioButton)findViewById(R.id.loginByUserName);
RadioButton loginByPwd = (RadioButton)findViewById(R.id.loginByPwd);
String DbUser,DbPwd;
if(loginByUserName.isChecked()){
DbUser = "zhangsan";
DbPwd = "123456";
}
else{
DbUser = "zhangsan@sample.com";
DbPwd = "123456";
}
EditText txtUserName = (EditText)findViewById(R.id.txtUserName);
EditText txtPwd = (EditText)findViewById(R.id.txtPwd);
TextView txtResult = (TextView)findViewById(R.id.txtResult);
if(txtUserName.getText().toString().equals(DbUser))
{
if(txtPwd.getText().toString().equals(DbPwd)){
//txtResult.setText("登录成功");
ToastShow("登录成功");
}
else{
//密码不存在
//txtResult.setText("密码有误");
//Toast.makeText(MainActivity.this, "密码有误", Toast.LENGTH_LONG).show();
ToastShow("密码有误");
}
}
else{
//用户名不存在
//txtResult.setText("用户名不存在");
//Toast.makeText(MainActivity.this, "用户名不存在", Toast.LENGTH_LONG).show();
ToastShow("用户名不存在");
}
}
});
}
void ToastShow(String Content){
Toast toast = Toast.makeText(MainActivity.this, Content, Toast.LENGTH_LONG);
//LENGTH_SHORT是弹出消息框的显示时间,默认是短的
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_template,null);
TextView txtToastContent = (TextView)view.findViewById(R.id.txtToastContent);
txtToastContent.setText(Content);
toast.setView(view);
toast.show();
}
}
PopupWindow的用途
package com.example.littleprogram;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton loginByUserName = (RadioButton)findViewById(R.id.loginByUserName);
RadioButton loginByPwd = (RadioButton)findViewById(R.id.loginByPwd);
String DbUser,DbPwd;
if(loginByUserName.isChecked()){
DbUser = "zhangsan";
DbPwd = "123456";
}
else{
DbUser = "zhangsan@sample.com";
DbPwd = "123456";
}
EditText txtUserName = (EditText)findViewById(R.id.txtUserName);
EditText txtPwd = (EditText)findViewById(R.id.txtPwd);
//TextView txtResult = (TextView)findViewById(R.id.txtResult);
if(txtUserName.getText().toString().equals(DbUser))
{
if(txtPwd.getText().toString().equals(DbPwd)){
//txtResult.setText("登录成功");
ToastShow("登录成功");
}
else{
//密码不存在
//txtResult.setText("密码有误");
//Toast.makeText(MainActivity.this, "密码有误", Toast.LENGTH_LONG).show();
ToastShow("密码有误");
}
}
else{
//用户名不存在
//txtResult.setText("用户名不存在");
//Toast.makeText(MainActivity.this, "用户名不存在", Toast.LENGTH_LONG).show();
ToastShow("用户名不存在");
}
}
});
final Button btnReg = (Button)findViewById(R.id.btnReg);
btnReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_content,null,false);
//contentView代表着我们登陆选项的布局设计
PopupWindow window = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,true);
window.setTouchable(true);
window.showAsDropDown(btnReg,0,0, Gravity.BOTTOM);
//window.showAtLocation(getWindow().getDecorView(),Gravity.BOTTOM,0,0);
}
});
}
void ToastShow(String Content){
Toast toast = Toast.makeText(MainActivity.this, Content, Toast.LENGTH_LONG);
//LENGTH_SHORT是弹出消息框的显示时间,默认是短的
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_template,null);
TextView txtToastContent = (TextView)view.findViewById(R.id.txtToastContent);
txtToastContent.setText(Content);
toast.setView(view);
toast.show();
}
}
package com.example.littleprogram;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton loginByUserName = (RadioButton)findViewById(R.id.loginByUserName);
RadioButton loginByPwd = (RadioButton)findViewById(R.id.loginByPwd);
String DbUser,DbPwd;
if(loginByUserName.isChecked()){
DbUser = "zhangsan";
DbPwd = "123456";
}
else{
DbUser = "zhangsan@sample.com";
DbPwd = "123456";
}
EditText txtUserName = (EditText)findViewById(R.id.txtUserName);
EditText txtPwd = (EditText)findViewById(R.id.txtPwd);
//TextView txtResult = (TextView)findViewById(R.id.txtResult);
if(txtUserName.getText().toString().equals(DbUser))
{
if(txtPwd.getText().toString().equals(DbPwd)){
//txtResult.setText("登录成功");
ToastShow("登录成功");
}
else{
//密码不存在
//txtResult.setText("密码有误");
//Toast.makeText(MainActivity.this, "密码有误", Toast.LENGTH_LONG).show();
ToastShow("密码有误");
}
}
else{
//用户名不存在
//txtResult.setText("用户名不存在");
//Toast.makeText(MainActivity.this, "用户名不存在", Toast.LENGTH_LONG).show();
ToastShow("用户名不存在");
}
}
});
final Button btnReg = (Button)findViewById(R.id.btnReg);
btnReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_content,null,false);
//contentView代表着我们登陆选项的布局设计
PopupWindow window = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,true);
window.setTouchable(true);
//window.showAsDropDown(btnReg,0,0, Gravity.BOTTOM);
window.showAtLocation(getWindow().getDecorView(),Gravity.BOTTOM,0,0);
}
});
}
void ToastShow(String Content){
Toast toast = Toast.makeText(MainActivity.this, Content, Toast.LENGTH_LONG);
//LENGTH_SHORT是弹出消息框的显示时间,默认是短的
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_template,null);
TextView txtToastContent = (TextView)view.findViewById(R.id.txtToastContent);
txtToastContent.setText(Content);
toast.setView(view);
toast.show();
}
}
Alert Dialog的应用
警告窗体
package com.example.littleprogram;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton loginByUserName = (RadioButton)findViewById(R.id.loginByUserName);
RadioButton loginByPwd = (RadioButton)findViewById(R.id.loginByPwd);
String DbUser,DbPwd;
if(loginByUserName.isChecked()){
DbUser = "zhangsan";
DbPwd = "123456";
}
else{
DbUser = "zhangsan@sample.com";
DbPwd = "123456";
}
EditText txtUserName = (EditText)findViewById(R.id.txtUserName);
EditText txtPwd = (EditText)findViewById(R.id.txtPwd);
//TextView txtResult = (TextView)findViewById(R.id.txtResult);
if(txtUserName.getText().toString().equals(DbUser))
{
if(txtPwd.getText().toString().equals(DbPwd)){
//txtResult.setText("登录成功");
ToastShow("登录成功");
}
else{
//密码不存在
//txtResult.setText("密码有误");
//Toast.makeText(MainActivity.this, "密码有误", Toast.LENGTH_LONG).show();
ToastShow("密码有误");
}
}
else{
//用户名不存在
//txtResult.setText("用户名不存在");
//Toast.makeText(MainActivity.this, "用户名不存在", Toast.LENGTH_LONG).show();
ToastShow("用户名不存在");
}
}
});
final Button btnReg = (Button)findViewById(R.id.btnReg);
btnReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("用户协议确认");
alert.setIcon(R.drawable.ic_launcher);
alert.setMessage("注册新用户需要接受用户协议,请您认真阅读用户协议内容,并选择是否同意接受用户协议");
alert.setButton(AlertDialog.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_content,null,false);
//contentView代表着我们登陆选项的布局设计
PopupWindow window = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,true);
window.setTouchable(true);
window.showAsDropDown(btnReg,0,0, Gravity.BOTTOM);
//window.showAtLocation(getWindow().getDecorView(),Gravity.BOTTOM,0,0);
}
});
alert.setButton(AlertDialog.BUTTON_NEGATIVE, "不同意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ToastShow("只有接受用户协议才能注册新用户");
}
});
alert.show();
}
});
}
void ToastShow(String Content){
Toast toast = Toast.makeText(MainActivity.this, Content, Toast.LENGTH_LONG);
//LENGTH_SHORT是弹出消息框的显示时间,默认是短的
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_template,null);
TextView txtToastContent = (TextView)view.findViewById(R.id.txtToastContent);
txtToastContent.setText(Content);
toast.setView(view);
toast.show();
}
}
CustomDialog的应用
自定义对话框
最常用的一种方法
package com.example.littleprogram;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton loginByUserName = (RadioButton)findViewById(R.id.loginByUserName);
RadioButton loginByPwd = (RadioButton)findViewById(R.id.loginByPwd);
String DbUser,DbPwd;
if(loginByUserName.isChecked()){
DbUser = "zhangsan";
DbPwd = "123456";
}
else{
DbUser = "zhangsan@sample.com";
DbPwd = "123456";
}
EditText txtUserName = (EditText)findViewById(R.id.txtUserName);
EditText txtPwd = (EditText)findViewById(R.id.txtPwd);
//TextView txtResult = (TextView)findViewById(R.id.txtResult);
if(txtUserName.getText().toString().equals(DbUser))
{
if(txtPwd.getText().toString().equals(DbPwd)){
//txtResult.setText("登录成功");
ToastShow("登录成功");
}
else{
//密码不存在
//txtResult.setText("密码有误");
//Toast.makeText(MainActivity.this, "密码有误", Toast.LENGTH_LONG).show();
ToastShow("密码有误");
}
}
else{
//用户名不存在
//txtResult.setText("用户名不存在");
//Toast.makeText(MainActivity.this, "用户名不存在", Toast.LENGTH_LONG).show();
ToastShow("用户名不存在");
}
}
});
final Button btnReg = (Button)findViewById(R.id.btnReg);
btnReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomDialog customDialog = new CustomDialog(MainActivity.this, new CustomDialog.OnCustomDialogListener(){
@Override
public void btnConfirmLicenseClicked(boolean isConfirm){
if(isConfirm == false){
ToastShow("只有接受用户协议才能注册新用户");
}
else{
ToastShow("感谢您成为我们的新用户");
}
}
});
customDialog.show();
/*
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("用户协议确认");
alert.setIcon(R.drawable.ic_launcher);
alert.setMessage("注册新用户需要接受用户协议,请您认真阅读用户协议内容,并选择是否同意接受用户协议");
alert.setButton(AlertDialog.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_content,null,false);
//contentView代表着我们登陆选项的布局设计
PopupWindow window = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,true);
window.setTouchable(true);
window.showAsDropDown(btnReg,0,0, Gravity.BOTTOM);
//window.showAtLocation(getWindow().getDecorView(),Gravity.BOTTOM,0,0);
}
});
alert.setButton(AlertDialog.BUTTON_NEGATIVE, "不同意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ToastShow("只有接受用户协议才能注册新用户");
}
});
alert.show();
*/
}
});
}
void ToastShow(String Content){
Toast toast = Toast.makeText(MainActivity.this, Content, Toast.LENGTH_LONG);
//LENGTH_SHORT是弹出消息框的显示时间,默认是短的
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_template,null);
TextView txtToastContent = (TextView)view.findViewById(R.id.txtToastContent);
txtToastContent.setText(Content);
toast.setView(view);
toast.show();
}
}