问题描述:

What I am trying to do is to have a simple sms application. The user puts their name and number in, and chooses to toggle a boolean box, and the enter button creates an intent to open up the built in sms and create a message according to the inputs.

What I'm having issues with is resetting the forms when I bring the application up again. Idealy I would like to have the entire application RESTART if you will, but Im not too sure how that works.

I was told to use:

editText.setText("");

to bring the fields to be null again, but eclipse isn't kind to me.

So my question is, how do I change my main_activity file with the code above to clear the edit text forms upon button press.

public class MainActivity extends Activity {

public boolean sexybox = false;

public void sexyBoolean(View view){ // changes the value from true to false etc

if(sexybox == false)

{

sexybox = true;

}

else

{

sexybox = false;

}

}

/** Called when the user clicks the Send button */

public void sendText(View view) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

//gives me user and number to be used.

EditText username = (EditText) findViewById(R.id.edit_name);

EditText usernumber = (EditText) findViewById(R.id.edit_number);

String usernamestring = username.getText().toString();

String usernumberstring = usernumber.getText().toString();

//checks if its null fields:

if(usernamestring.isEmpty())

{

Context context = getApplicationContext();

CharSequence text = "What's your name?";

int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);

TextView v = (TextView) toast.getView().findViewById(android.R.id.message);

v.setTextColor(Color.CYAN);

toast.show();

return;

}

if(usernumberstring.isEmpty())

{

Context context = getApplicationContext();

CharSequence text = "What's your numbah?";

int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);

TextView v = (TextView) toast.getView().findViewById(android.R.id.message);

v.setTextColor(Color.CYAN);

toast.show();

return;

}

///

System.out.println("SENDING MESSAGE:");

System.out.println(usernamestring);

System.out.println(usernumberstring);

String body = "Hi Jake - I'm " + usernamestring + "! I'm sending a self text so we can talk or whatever. ";

//IF CUTE button toggled

if(sexybox == true)

{

body = body + "I think you're hot too ;)";

}

smsIntent.putExtra("sms_body", body); //obvi the message

smsIntent.putExtra("address", usernumberstring); //obvi the number, replace usernumberstring

smsIntent.setType("vnd.android-dir/mms-sms"); // guess i leave this alone

//RESET FIELDS ????

// EditText edit_name = (EditText) findViewById(R.id.edit_name);

// edit_name.setText("");

// editText.setText("");

//editText.setText(null);

startActivity(smsIntent);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

// XML:

<TextView

android:id="@+id/toptext"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#303030"

android:text="@string/toptext"

android:textAppearance="?android:attr/textAppearanceLarge"

android:textColor="#33B5E5"

android:textSize="50sp" />

<TextView

android:id="@+id/toptext2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#303030"

android:text="@string/toptext2"

android:textAppearance="?android:attr/textAppearanceLarge"

android:textColor="#33B5E5" />

<EditText

android:id="@+id/edit_name"

android:layout_width="match_parent"

android:layout_height="75dp"

android:ems="10"

android:hint="@string/edit_name" />

<EditText

android:id="@+id/edit_number"

android:layout_width="match_parent"

android:layout_height="137dp"

android:ems="10"

android:hint="@string/edit_number"

android:inputType="phone" />

<TextView

android:id="@+id/thanks"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:clickable="true"

android:text="@string/thanks"

android:textAppearance="?android:attr/textAppearanceLarge"

android:textColor="#33B5E5" />

<CheckBox

android:id="@+id/sexycheckbox"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="sexyBoolean"

android:text="@string/sexycheckbox" />

<Button

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#303030"

android:onClick="sendText"

android:text="@string/button_send"

android:textColor="@android:color/white" />

</LinearLayout>

相关阅读:
Top