zoukankan      html  css  js  c++  java
  • 随手记(三)

    记一笔
    界面

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="100dp"
        android:layout_marginRight="250dp"
        android:layout_weight="2"
        android:id="@+id/linearLayout">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="类型"
            android:id="@+id/texttype"
    
            android:layout_gravity="center_horizontal"
            android:textStyle="bold"
            android:clickable="false"
            android:editable="false"
            android:textColor="#0472f8"
            android:layout_weight="0.00"
            android:layout_marginBottom="50dp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="时间"
            android:id="@+id/texttimeshow"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="50dp"
            android:textColor="#0472f8" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="费用"
            android:id="@+id/textfeeshow"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="50dp"
            android:textColor="#0472f8" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="备注"
            android:id="@+id/textremarksshow"
            android:layout_gravity="center_horizontal"
            android:textColor="#0472f8" />
    </LinearLayout>
    
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignTop="@+id/linearLayout"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="100dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="110dp"
        android:weightSum="1"
        android:id="@+id/linearLayout2">
    
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:id="@+id/spinner"
            android:spinnerMode="dropdown"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="5dp"
            android:entries="@array/Select_item"
            android:layout_marginBottom="40dp" />
    
        <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:background="@android:drawable/edit_text"
            android:layout_marginTop="0dp" />
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="@android:drawable/edit_text"
            android:layout_marginTop="30dp" />
        <EditText android:layout_width="fill_parent"
            android:layout_height="94dp"
            android:inputType="textMultiLine"
            android:singleLine="false"
            android:gravity="left|top"
            android:width="2dip"
            android:layout_marginTop="40dp"
            android:background="@android:drawable/edit_text"
            />
    </LinearLayout>
    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="400dp"
        android:layout_marginBottom="40dp"
        android:clipToPadding="false"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="10dp">
    
        <Button
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:text="保存"
            android:id="@+id/plan_sure"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="38dp"
            android:background="#a8d093" />
    
        <Button
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:text="取消"
            android:onClick="BackMain"
            android:id="@+id/button_cancel"
            android:layout_marginTop="38dp"
            android:layout_marginLeft="160dp"
            android:background="#a8d093" />
    </LinearLayout>
    
    java代码部分 private Button mbutton_sure, mbutton_cancel; private Spinner mspinner_type; private EditText medittext_time, medittext_fee, medittext_remarks; //保存类型数据 private String content_type; private ArrayList Data = new ArrayList();
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
                View view = inflater.inflate(R.layout.recorder, container, false);
        /*4.0版本过后需要操作http请求需要我们重新
        *设计在4.0之后在主线程里面执行Http请求都会报这个错,也许是怕Http请求时间太长造成程序假死的情
        * */
                StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
                StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
    
                //获得控件
                mbutton_sure = (Button) view.findViewById(R.id.plan_sure);
                mbutton_cancel = (Button) view.findViewById(R.id.button_cancel);
                medittext_time = (EditText) view.findViewById(R.id.edit_text_time);
                medittext_fee = (EditText) view.findViewById(R.id.editText_fee);
                medittext_remarks = (EditText) view.findViewById(R.id.editText_remarks);
                mbutton_sure = (Button) view.findViewById(R.id.plan_sure);
                mspinner_type = (Spinner) view.findViewById(R.id.spinner_type);
    
    
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                        R.array.Select_item, android.R.layout.simple_spinner_item);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
                mspinner_type.setAdapter(adapter);
    
                mspinner_type.setOnItemSelectedListener(listener);
                mbutton_sure.setOnClickListener(this);
                mbutton_cancel.setOnClickListener(this);
                return view;
        }
    
        OnItemSelectedListener listener = new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                        // An item was selected. You can retrieve the selected item using
                        content_type = mspinner_type.getItemAtPosition(pos).toString();
                        //     Toast.makeText(getActivity(), "选中了"+content_type, Toast.LENGTH_LONG).show();
                }
    
                public void onNothingSelected(AdapterView<?> parent) {
                        // Another interface callback
                }
        };
    
        public void onClick(View view) {
    
                switch (view.getId()) {
                        case R.id.plan_sure:
                                Data.clear();
                                Data.add(content_type);
                                Data.add(medittext_time.getText().toString());
                                Data.add(medittext_fee.getText().toString());
                                Data.add(medittext_remarks.getText().toString());
                                //    Toast.makeText(getActivity(), "click sure button" + Data, Toast.LENGTH_LONG).show();
                                WriteData(Data);
                                //   Toast.makeText(getActivity(), "click sure button" + Data, Toast.LENGTH_LONG).show();
                                break;
                        case R.id.button_cancel:
                                GetData();
                                break;
                        default:
                                break;
                }
        }
    
        public void GetData() {
                HttpURLConnection connection = null;
    
                try {
                        //设置url
                        URL url = new URL("http://10.135.188.23:8080/test.php");
                        //获得url连接
                        connection = (HttpURLConnection) url.openConnection();
                        //设置可写,默认为false
                        connection.setDoOutput(true);
                        //设置可读,默认为true,一般不写
                        connection.setDoInput(true);
                        //设置POST传参,默认为GET
                        connection.setRequestMethod("POST");
    
                        connection.setUseCaches(false);
                        connection.setInstanceFollowRedirects(true);
                        connection.setRequestProperty("contentType", "text/html");
                        connection.connect();
    
                        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                        String content = "username=" + URLEncoder.encode("zcl", "utf-8");
                        out.writeBytes(content);
                        //刷新
                        out.flush();
    
                        out.close();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
                        //设置编码,否则中文乱码
                        String line = "";
                        System.out.println(reader);
                        while ((line = reader.readLine()) != null) {
                                Toast.makeText(getActivity(), line.toString(), Toast.LENGTH_LONG).show();
                        }
                        reader.close();
                        connection.disconnect();
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        connection.disconnect();
                }
    
        }
    
        public void WriteData(ArrayList<String> Data) {
                String outputFile = "//a.xls";
                try {
    
                        // 判断是否存在SD卡
                        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                        } else {
                                Toast.makeText(getActivity(), "sd卡不存在", Toast.LENGTH_LONG).show();
                                return;
                        }
    
                        String sdCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
                        File file = new File(sdCardRoot + outputFile);
                        if (!file.exists()) {
    
                                // 创建新的Excel 工作簿
                                HSSFWorkbook workbook = new HSSFWorkbook();
    
                                // 在Excel工作簿中建一工作表,其名为缺省值
                                // 如要新建一名为"效益指标"的工作表,其语句为:
                                // HSSFSheet sheet = workbook.createSheet("效益指标");
                                HSSFSheet sheet = workbook.createSheet("消费记录");
                                // 在索引0的位置创建行(最顶端的行)
                                HSSFRow row = sheet.createRow((short) 0);
    
                                //创建表头
                                HSSFCell empCodeCell = row.createCell(0);
                                empCodeCell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                empCodeCell.setCellValue("类型");
    
                                HSSFCell empNameCell = row.createCell(1);
                                empNameCell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                empNameCell.setCellValue("时间");
    
                                HSSFCell sexCell = row.createCell(2);
                                sexCell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                sexCell.setCellValue("费用");
    
                                HSSFCell birthdayCell = row.createCell(3);
                                birthdayCell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                birthdayCell.setCellValue("备注");
    
                                //写入数据
                                int num = sheet.getLastRowNum();
                                //   Toast.makeText(getActivity(), num, Toast.LENGTH_LONG).show();
                                row = sheet.createRow(num + 1);
                                for (int i = 0; i < 4; i++) {
    
                                        empCodeCell = row.createCell(i);
                                        empCodeCell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                        empCodeCell.setCellValue(Data.get(i));
                                }
    
                                // 新建一输出文件流
                                FileOutputStream fOut = new FileOutputStream(sdCardRoot + outputFile);
                                // 把相应的Excel 工作簿存盘
                                workbook.write(fOut);
                                fOut.flush();
                                // 操作结束,关闭文件
                                fOut.close();
                                workbook.close();
                        } else {
                                //更改数据
                                try {
                                        FileInputStream fs = new FileInputStream(sdCardRoot + outputFile);
                                        POIFSFileSystem ps = new POIFSFileSystem(fs);
                                        // 使用POI提供的方法得到excel的信息
                                        HSSFWorkbook wb = new HSSFWorkbook(ps);
                                        HSSFSheet sheet = wb.getSheetAt(0);
                                        // 获取到工作表,因为一个excel可能有多个工作表
    
                                        FileOutputStream out = new FileOutputStream(sdCardRoot + outputFile);
                                        HSSFCell empCodeCell = null;
                                        //写入数据
                                        int num = sheet.getLastRowNum();
                                        //   Toast.makeText(getActivity(), num, Toast.LENGTH_LONG).show();
                                        HSSFRow row = sheet.createRow(num + 1);
                                        for (int i = 0; i < 4; i++) {
                                                empCodeCell = row.createCell(i);
                                                empCodeCell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                                empCodeCell.setCellValue(Data.get(i));
                                        }
                                        out.flush();
                                        wb.write(out);
                                        out.close();
    
                                } catch (Exception e) {
                                        Toast.makeText(getActivity(), "写入失败" + e, Toast.LENGTH_LONG).show();
                                }
    
                        }
                } catch (Exception e) {
                        Toast.makeText(getActivity(), "写入失败" + e, Toast.LENGTH_LONG).show();
                } finally {
                        Toast.makeText(getActivity(), "关闭", Toast.LENGTH_LONG).show();
                }
        }
    

  • 相关阅读:
    基于python的socket网络编程
    Python3报错:ModuleNotFoundError: No module named '_bz2'
    机器学习博客网站
    《Linux内核设计与实现》 读书笔记(4)--进程的调度
    k8s 简单入门
    docker 简单入门
    python3 教程
    .toml 文件简介
    编码规范
    python3 基本用法
  • 原文地址:https://www.cnblogs.com/liqinsqzr/p/7007234.html
Copyright © 2011-2022 走看看