Android 动态生成对话框和EditText

/**
     * (获取输入)
     */
    private void showInputDialog() {

       ScrollView scrollview = getInitView() ;
       final LinearLayout layout = (LinearLayout)
               scrollview.findViewById(30) ;

       new AlertDialog.Builder(this)
           .setTitle("请输入")
           .setIcon(android.R.drawable.ic_dialog_info)
           .setView(scrollview)
           .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         setNewModule(layout) ;
                     }
                })
           .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       // do nothing
                   }
               })
           .show(); 

    }

    /**
     * (动态设置view)
     */
    private ScrollView getInitView() {
      //新建一个布局
        LinearLayout layout = new LinearLayout(this);

        LinearLayout.LayoutParams layoutParams =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);

        //设置为纵向布局
        layout.setOrientation(LinearLayout.VERTICAL) ;
        //设置layout大小
        layout.setLayoutParams(layoutParams) ;
        //设置layout Id
        layout.setId(30) ;

        //新建编辑框
        EditText titleEditText = new EditText(this);
        titleEditText.setId(20) ;
        titleEditText.setHint("请输入title:");
     //要添加更多对话框可以照着例子添加

        //把编辑框加入到layout中
        layout.addView(titleEditText) ;

        //给layout包一层可滚动的scrollview
        ScrollView scrollview = new ScrollView(this) ;
        scrollview.setLayoutParams(layoutParams) ;
        scrollview.addView(layout) ;

        return scrollview ;
    }

    /**
     * (设置module的值)
     * @param layout
     */
    private void setNewModule(LinearLayout layout) {

        String title = ((EditText)layout.findViewById(20))
                .getText().toString() ;
        //此处省略掉一些代码
        //设置module
        Module module = new Module() ;
        module.title = title;

    } 

效果图大致如下:

时间: 2024-08-09 09:30:54

Android 动态生成对话框和EditText的相关文章

Android 动态生成布局 (多层嵌套)

Android 除了可以加载xml文件,显示布局外,也可以代码生成布局,并通过setContentView(View view)方法显示布局.单独的一层布局,如一个主布局加一个控件(如Button\imageView等)动态生成代码比较简单,下面只给出示例代码: package com.example.android_dongtaishengcheng; import android.os.Bundle; import android.app.Activity; import android.c

Android动态生成表格

最近刚刚学习完Android的五大布局,现在我们进一步深入学习,尝试做一个动态生成表格功能的例子 样式布局代码如下: 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4

Android动态生成课程表 详解

根据提供的课程信息,动态生成课程表.不同于网上流传的课表形式,课程节数是固定,本课表的课程节数不固定. 1.效果图 每天共有12节课,上课节数每天都不同. 2.布局文件代码 周一到周日是  7个竖直线性布局文件,其他皆为辅助标题或序号. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools

Android动态生成按钮样式

动态生成按钮样式 使用: int borderColor = Color.parseColor("#2E3135"); int bgColor = Color.parseColor("#00FF00"); // 设置View背景样式,有边框宽度.边框颜色.圆角度数.背景颜色. GradientDrawable shape = DrawableUtils.createShape(1, 4, borderColor, bgColor); btn1.setBackgrou

android动态生成界面、添加组件

效果图: layout界面布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:ad="http://schemas.android.com/apk/res/android" ad:layout_width="match_parent" ad:layout_height="match_parent" ad:orient

Android动态类生成预加载-dexmaker使用

一.dexmaker简单介绍 dexmaker是运行在Android Dalvik VM上,利用Java编写,来动态生成DEX字节码的API.如果读者了解AOP编程的话,应该听说过cglib or ASM,但这两个工具生成都是Java字节码,而Dalvik加载的必须是DEX字节码.所以,想要在Android上进行AOP编程,dexmaker可以说是一个很好的选择.项目地址:https://github.com/crittercism/dexmaker. 二.简单使用 下面这个例子非常典型,可以说

Android 动态改变高度以及计算长度的EditText

前段时间项目需求,需要做一个有限制长度的输入框并动态显示剩余文字,同时也要动态改变EditText的高度来增加用户体验.现整理出来与大家分享. 先来看看效果图 看了效果就分享一下布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_wid

android 动态背景的实现以及SurfaceView中添加EditText控件

      首先还是一贯作风,我们先看案例: 静态图看不出来效果,如果用过此软件(扎客)的同学们都知道,她的背景会动.怎么样,是不是觉得很时尚,起码比静态的要好(个人观点).其实实现起来并 不复杂,这个如果让做游戏程序员做简直太小儿科了,这里我说明一点,其实我们做应用的也应该多少了解下游戏编程思维,起码对我们做应用有很好的帮助. 下面我简单介绍下实现方式. 实现原理:自定义一个SurfaceView控件.对之不停的onDraw,使得其背景动起来. 对于SurfaceView如果不了解的同学们麻烦

Android Studio - 第四十七期 毛玻璃效果以及动态生成二维码以及增大点击热区

最近回看撸撸的代码,有一些自定义的view写法很不错,下面封装出来,希望能帮到大家: 1.毛玻璃效果:BitmapUtils package com.example.p030_popbgqcode.utils; import android.content.Context; import android.graphics.Bitmap; import android.renderscript.Allocation; import android.renderscript.Element; imp