Android创建文件夹

时间:2016-03-16 17:05:28

标签: android

我正在尝试点击按钮创建一个文件夹。但是,当我在手机中调试我的应用程序时,我认为它的工作原理并没有在外部存储中创建文件夹。

Java代码

package com.scoobydrive.project;

import android.os.Bundle; 
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class signup extends AppCompatActivity {
    Button b;
    TextView text1, text2, text3;
    EditText editText1, editText2, editText3;
    RadioButton r1, r2;
    String Name, age, phonenumber,gender;
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        b = (Button) findViewById(R.id.submitbutton);
        text1 = (TextView) findViewById(R.id.nameTextView);
        text2 = (TextView) findViewById(R.id.agetextView);
        text3 = (TextView) findViewById(R.id.phnumtextView);
        editText1 = (EditText) findViewById(R.id.namefield);
        editText2 = (EditText) findViewById(R.id.ageeditText);
        editText3 = (EditText) findViewById(R.id.phnumeditText);
        r1 = (RadioButton) findViewById(R.id.maleradioButton);
        r2 = (RadioButton) findViewById(R.id.femaleradioButton);
    }

    File folder=new File(Environment.getExternalStorageDirectory()+ File.separator+"TESTING");
    public void confirm(View v) {
        if (!folder.exists() && !folder.isDirectory()) {
            folder.mkdirs();
            Name = text1.getText().toString();
            age = text2.getText().toString();
            phonenumber = text3.getText().toString();
            gender = "";

            if (r1.isChecked())
                gender = "MALE";
            else if (r2.isChecked())
                gender = "FEMALE";
            else
                Toast.makeText(getApplicationContext(), "Select Gender", Toast.LENGTH_LONG).show();

            if (age.length() > 0 && Name.length() > 0 && phonenumber.length() > 0) {
                Toast.makeText(getApplicationContext(), "OTP Sent", Toast.LENGTH_LONG).show();
            } else
                Toast.makeText(getApplicationContext(), "Please Fill All Fields", Toast.LENGTH_LONG).show();

        }
    }
}

content_XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.scoobydrive.project.signup"
tools:showIn="@layout/activity_signup"
android:orientation="vertical"
android:background="#F5af3d">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/name_text_view"
    android:id="@+id/nameTextView"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/namefield"
    android:layout_toStartOf="@+id/namefield" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/namefield"
    android:layout_alignTop="@+id/nameTextView"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/age_text_view"
    android:id="@+id/agetextView"
    android:layout_marginTop="34dp"
    android:layout_below="@+id/namefield"
    android:layout_alignLeft="@+id/nameTextView"
    android:layout_alignStart="@+id/nameTextView" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberSigned"
    android:ems="10"
    android:maxLength="3"
    android:id="@+id/ageeditText"
    android:layout_alignBottom="@+id/agetextView"
    android:layout_alignLeft="@+id/namefield"
    android:layout_alignStart="@+id/namefield" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/phone_text_view"
    android:id="@+id/phnumtextView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:id="@+id/phnumeditText"
    android:maxLength="10"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/radioGroup" />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/submit_edit_text"
    android:id="@+id/submitbutton"
    android:background="#f6c86c"
    android:onClick="confirm"
    android:layout_marginBottom="77dp"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/phnumtextView"
    android:layout_toEndOf="@+id/phnumtextView" />
<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radioGroup"
    android:layout_above="@+id/phnumeditText"
    android:layout_alignLeft="@+id/ageeditText"
    android:layout_alignStart="@+id/ageeditText"
    android:layout_marginLeft="34dp"
    android:layout_marginStart="34dp">


<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/male_radio_button"
    android:id="@+id/maleradioButton"
    android:checked="false"
    android:layout_alignTop="@+id/femaleradioButton"
    android:layout_alignLeft="@+id/ageeditText"
    android:layout_alignStart="@+id/ageeditText" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/female_radio_button"
    android:id="@+id/femaleradioButton"
    android:checked="false"
    android:layout_above="@+id/phnumeditText"
    android:layout_alignRight="@+id/ageeditText"
    android:layout_alignEnd="@+id/ageeditText"
    android:layout_marginBottom="50dp" />
   </RadioGroup>
  </RelativeLayout>

主要节日代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.scoobydrive.project">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".signup"
        android:label="@string/title_activity_signup"
        android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

</manifest>

0 个答案:

没有答案