Я пытаюсь использовать новое окно и присоединяю текст редактирования, чтобы получить имя от пользователя.Просто он всегда терпит неудачу, когда попадает в строку "" name " .getText () .toString ()"
Вот мой главный
public class chatter extends Activity {
private String name;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.setNameMenu:
setname();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void setname()
{
final EditText textNameInput = (EditText) findViewById(R.id.nameBox);
/*Dialog dialog = new Dialog(chatter.this);
dialog.setTitle("This is my custom dialog box");
*/
setContentView(R.layout.custom_dialog);
//dialog.setCancelable(true);
Button button = (Button) findViewById(R.id.submitNameButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//String nameinput = textNameInput.getText().toString();
**//this is where it does not work**
**Editable debug1 = textNameInput.getText();**
String nameinput = debug1.toString();
name = nameinput;
finish();
}
});
}
}
Вотмакет
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<TextView
android:id="@+id/dialogueText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="please input your name:"
/>
<EditText
android:id="@+id/nameBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dialogueText" />
<Button
android:text=" Set "
android:id="@+id/submitNameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameBox"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
Я новичок в программировании на Android, пожалуйста, объясните, пожалуйста, что пошло не так.