美文网首页
alertdialog 自定义view 以及反射

alertdialog 自定义view 以及反射

作者: gaom明 | 来源:发表于2018-05-11 15:31 被阅读11次
      View  view = (LinearLayout) getLayoutInflater().inflate(R.layout.outlayout,null);
            AlertDialog.Builder builder =new AlertDialog.Builder(DataActivity1.this);
            builder.setTitle("导出数据");
            builder.setView(view);


            final EditText 文件名 = (EditText)view.findViewById(R.id.fileName);
            文件名.setText(nowTime);
                    //相当于点击确认按钮
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            String fileName = 文件名.getText() + "";
                            if (fileName.length() < 1){
                                try {
                                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                    field.setAccessible(true);
                                    field.set(dialog, false);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }else{
                                try {
                                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                    field.setAccessible(true);
                                    field.set(dialog, true);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }


                                ExcelOperTest ex = new ExcelOperTest();
                                try {
                                    ex.DataWrite2Excel(DataActivity1.dataList,fileName + ".xls");
                                    Toast.makeText(DataActivity1.this, "导出成功!",
                                            Toast.LENGTH_SHORT).show();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                    Toast.makeText(DataActivity1.this, "导出失败!",
                                            Toast.LENGTH_SHORT).show();
                                } catch (WriteException e) {
                                    e.printStackTrace();
                                    Toast.makeText(DataActivity1.this, "导出失败!",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }

                        }
                    });
    //相当于点击取消按钮
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub


                        }
                    });

            builder.create();
            builder.show();

//布局

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:orientation="vertical">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                android:padding="5dp"
                android:text="11111111111111111"
                tools:text="11111111111"
                android:id="@+id/fileName" />


            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="0dp"
                android:text=".xls"
                android:id="@+id/样方代码textView"
                android:textColor="@android:color/black" />
        </LinearLayout>


    </LinearLayout>

</LinearLayout>
</LinearLayout>

相关文章

网友评论

      本文标题:alertdialog 自定义view 以及反射

      本文链接:https://www.haomeiwen.com/subject/nqifdftx.html