我要做的是拥有一个简单的短信应用程序。用户输入他们的姓名和号码,并选择切换布尔值框,然后输入按钮创建打开内置短信的意图并根据输入创建消息。
我遇到的问题是当我再次启动应用程序时重置表单。理想我想拥有整个应用程序RESTART,但是我不太确定它是如何工作的。
我被告知使用: editText.setText( “”); 将字段再次置为空,但是eclipse对我不好。 所以我的问题是,如何使用上面的代码更改main_activity文件,以便在按下按钮时清除编辑文本表单。
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>