Android java:putExtra和getStringExtra

时间:2014-03-15 12:02:12

标签: java android

(DatePickerActivity)>> 选择音符类型(SelectTypeActivity)>> 来编写txt文件(WriteNoteActivity)。这是流程。但我必须将意图 SelectTypeActivity.class 更改为 WriteNoteActivity.class ,这样才会成功,我怎样才能跳过 SelectTypeActivity 还能做到吗? =)

DatePickerActivity =创建文件夹

 public void onClick(DialogInterface dialog, int which) { 

                          Environment.getExternalStorageDirectory();
                             String dateN = edit_date.getText().toString();


                             edit_date.setTypeface(edit_date.getTypeface(), Typeface.BOLD_ITALIC);
                                File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/CalendarNote/" + dateN);
                                boolean success = true;
                                    if (!folder.exists()) {
                                        success = folder.mkdirs();


                                    }
                                    if (success) {
                                            Toast.makeText(getBaseContext(), "You have successfully created." , Toast.LENGTH_LONG ).show();
                                            Intent w = new Intent(DatePickerActivity.this, WriteNoteActivity.class);
                                            w.putExtra("the_date", dateN);
                                            startActivity(w);


                                    } else {
                                        Toast.makeText(getBaseContext(), "You have Failed to create." , Toast.LENGTH_LONG ).show();
                                    }

                        }
                     })

WriteNoteActivity =创建txt文件

        public void onClick(DialogInterface dialog, int which) { 
             String content = edit_content.getText().toString();
             String title = edit_title.getText().toString();
             //String dateN = edit_date.getText().toString();


             boolean success = true;
                 try {
                    String dateN = getIntent().getStringExtra("the_date");
                    File sdCardDir = Environment.getExternalStorageDirectory();  
                    File targetFile;

                    targetFile = new File(sdCardDir.getCanonicalPath()  
                            + "/CalendarNote/" + dateN );


                    // File file=new File(folderPath + "/"+title+".txt");
                    File file=new File(targetFile + "/"+title+".txt");

                    if(!targetFile.exists()){
                        success = targetFile.mkdir();  
                    }
                    RandomAccessFile raf = new RandomAccessFile(file, "rw");  
                    raf.seek(file.length());  

                    raf.write(content.getBytes());  
                    raf.close();  
                } catch (IOException e) {
                    e.printStackTrace();
                } 
                 if (success) {
                    Toast.makeText(getBaseContext(), "You have successfully created." , Toast.LENGTH_LONG ).show();



            } else {
                Toast.makeText(getBaseContext(), "You have Failed to create." , Toast.LENGTH_LONG ).show();
            }
                    //Toast.makeText(getBaseContext(), "Note have successfully saved." , Toast.LENGTH_LONG ).show();
                }
             })

是否有任何建议不要跳过SelectTypeActivity?也可以从DatePickerActivity获取getStringExtra

1 个答案:

答案 0 :(得分:0)

我会将SelectTypeActivity更改为一个对话框,如果此活动的唯一目的是显示不同类型的注释,则可以通过编程方式在对话框上执行对话(而不是转到SelectTypeActivity活动):

Dialog dialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);

builder.setView(modeList);
dialog = builder.create();

然后使用.setPositiveButton

处理onclick按钮

点击此处查看详情:How to click 'OK' on an AlertDialog via code?

很明显,您必须使用您的笔记类型填充modeList

希望我能帮助你:)。