将布局文件设置到对话框里面

将布局文件设置到对话框里面

(1)实现xml文件变为dialog里面的内容

(2)ProgressDialog进程对话框的简单应用

activity_main.xml文件

<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" >  

    <Button  
        android:id="@+id/<strong><span style="color:#000099;">login</span></strong>"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="100dp"  
        android:text="@string/login" />  

</RelativeLayout>

新建xml登录布局文件用于弹出对话框用户输入用户名和密码

login.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/inputname" />  

    <EditText  
        android:id="@+id/<strong><span style="color:#000099;">user</span></strong>"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:inputType="text" >  
    </EditText>  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/inputpass" />  

    <EditText  
        android:id="@+id/<strong><span style="color:#000099;">password</span></strong>"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:ems="10"  
        android:inputType="textPassword" >  
    </EditText>

</LinearLayout>  
string.xml代码

<resources>  

    <string name="app_name">DialogTest</string>  
    <string name="login">进入登录页面</string>  
    <string name="menu_settings">Settings</string>  
    <string name="title_activity_main">主界面</string>  
    <string name="inputname">请输入你的用户名:</string>  
    <string name="inputpass">请输入你的密码:</string>  
    <string name="hello_world">Hello world!</string>  
    <string name="title_activity_success">登录成功页</string>  

</resources>  
Main_Activity.java代码

public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

<span style="color:#33cc00;">//查找登录按钮组件  
span>       login = (Button) this.findViewById(R.id.login);  
<span style="color:#009900;">//注册监听,点击按钮打开登录对话框</span>  
[java] view plaincopyprint?
        login.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                <span style="color:#ff9900;">//显示登录窗口  
</span>             showDialog(LOGIN);  
            }  
        });  
    }  


// 创建对话框
@Override  
    protected Dialog onCreateDialog(int id) {  
        //LayoutInflater将xml的布局文件变成一个View实例  
        <strong>LayoutInflater inflater=LayoutInflater.from(this);</strong>  
        //布局为logindialog实例  
        View logindialog=inflater.inflate(R.layout.login, null);  
        //NEW出alertdialog  
        AlertDialog.Builder builder=new AlertDialog.Builder(this);  
        builder.setIcon(android.R.drawable.ic_menu_help).setTitle("请输入").setView(logindialog);  
        //查找已布局到对话框内View--logindialog的用户名和密码组件  
        <strong>name=(EditText) logindialog.findViewById(R.id.user);  
        pass=(EditText) logindialog.findViewById(R.id.password);</strong>  

        builder.setPositiveButton("登录", new DialogInterface.OnClickListener() {  
            //点击登录时事件处理  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                System.out.println(name.getText());  
                System.out.println(pass.getText());  
                //转化为String类型,否则出现空指针异常  
                if(name.getText().toString().equals("admin")&&pass.getText().toString().equals("123")){  
                    //创建进程对话框  
                    ProgressDialog pro=ProgressDialog.show(MainActivity.this, "请等待","正在为你登录……", true);  
                    try {  
                        //进程休眠三秒钟  
                        Thread.sleep(3000);  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                    Intent intent=new Intent();  
                    intent.setClass(MainActivity.this,SuccessActivity.class);  
                    //将登录的用户名传到另一个activity——SuccessActivity  
                    intent.putExtra("username", name.getText().toString());  
                        //必须转化为String类型,否则出现空指针  
                    startActivity(intent);  
                }  
                else  
                    Toast.makeText(MainActivity.this, "登录失败!!!\n用户名或密码不正确", Toast.LENGTH_LONG).show();  
            }  
        });  


        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){  
            //取消登录时退出登录  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                removeDialog(LOGIN);//关闭对话框  
            }  
        });  
        return builder.create();  
    }  
activity_success.xml布局文件代码

<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" >  

    <TextView  
        android:id="@+id/<span style="color:#cc0000;">showuser</span>"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerHorizontal="true"  
        android:padding="@dimen/padding_medium"  
        tools:context=".SuccessActivity" />  

</RelativeLayout>  


Success_Activity.java代码

[java] view plaincopyprint?
public class SuccessActivity extends Activity {  
    private TextView showinfo;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_success);  
        showinfo=(TextView) this.findViewById(R.id.showuser);  
        //获取定义的意图,得到传的参数值  
          <strong>Intent i=this.getIntent();</strong>  
        showinfo.setText("登录成功!!!\n欢迎"+i.getStringExtra("username")+"登录!");  
    }  

    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        getMenuInflater().inflate(R.menu.activity_success, menu);  
        return true;  
    }      
}  

运行效果:

这里写图片描述

用户名及密码不正确或是有空值时登录失败,不跳转其他页面

这里写图片描述

这里写图片描述

输入正确时显示进程对话框,并进行登录跳转成功页面

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值