zoukankan      html  css  js  c++  java
  • android 他们定义对话框

    创建一个布局文件 my_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        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:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="4dp"
            android:text="@string/textView1"
            />
        
        <EditText 
            android:id="@+id/editText_user"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="4dp"
            />
        
         <TextView 
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
       		android:layout_alignLeft="@+id/editText_user"
            android:layout_below="@+id/editText_user"
            android:layout_marginTop="7dp"
            android:text="@string/textView2"
            />
        
        <EditText 
            android:id="@+id/editText_pwd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView2"
            android:layout_marginTop="4dp"
            android:layout_below="@+id/textView2"
            />
    </RelativeLayout>

    
    

    下面是strings.xml文件:

    <?

    xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">demo091801</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="MyDialog">自己定义对话框</string> <string name="textView1">username</string> <string name="textView2">password</string> </resources>



    在主activity_main.xml中加入一个button控件,当单击此button时即弹出自己定义的对话框:

    <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" >
    
      <Button 
          android:id="@+id/My_Dialog"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/MyDialog"
          />
    </RelativeLayout>
    
    图形界面例如以下:


    在MainActivity.java代码中为button控件设置单击事件监听,并弹出自己定义的对话框:

    MainActivity.java:

    package com.example.demo091801;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		Button btn1 = (Button)findViewById(R.id.My_Dialog);
    		
    		btn1.setOnClickListener(new OnClickListener()
    		{
    
    			@Override
    			public void onClick(View arg0) {
    				// TODO Auto-generated method stub
    				
    				
    				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    				LayoutInflater inflater = getLayoutInflater();
    				View view = inflater.inflate(R.layout.my_dialog, null);
    				
    				builder.setTitle("登陆");
    				builder.setView(view);
    				builder.setPositiveButton("登陆", new DialogInterface.OnClickListener() {
    					
    					@Override
    					public void onClick(DialogInterface arg0, int arg1) {
    						// TODO Auto-generated method stub
    						
    					}
    				});
    				
    				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    					
    					@Override
    					public void onClick(DialogInterface arg0, int arg1) {
    						// TODO Auto-generated method stub
    						
    					}
    				});
    				
    						
    						
    				
    				AlertDialog dialog = builder.create();
    				dialog.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;
    	}
    
    }
    




    作者:http://blog.csdn.net/lp310018931

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Tornado-Lesson06-ORM、SQLAlchemy连接数据库、Module和增删改查
    Tornado-Lesson04-模版、模版转义、静态文件的引用
    Tornado-Lesson05-模版继承、函数和类导入、ui_methods和ui_modules
    博弈论
    给图片添加水印
    Apsara Clouder专项技能认证:实现调用API接口
    好用的工具
    书写是为了更好的思考
    使用与破解ExcelVBA密码
    一款可以直接下载浏览器sources资源的Chrome插件
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4878292.html
Copyright © 2011-2022 走看看