как получить текст из редактировать текст в Android? - PullRequest
1 голос
/ 19 июня 2010

Я использую следующий код для получения значений из текстового поля. Но он не работает, он дает исключение и принудительно закрывается только. Как получить значение текстового поля? Кто-нибудь может мне помочь?

public class alertwithlogin extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    showDialog(1);
}
@Override
protected Dialog onCreateDialog(int id) {
  LayoutInflater factory = LayoutInflater.from(this); 
     final View textEntryView = factory.inflate(R.layout.alertedit, null); 
     return new AlertDialog.Builder(alertwithlogin.this)             
         .setTitle(R.string.alert_dialog_text_entry)
         .setView(textEntryView)
         .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int whichButton) {
              EditText edittext = (EditText) findViewById(R.id.username_edit); 
              Toast.makeText(getApplicationContext(),edittext.getText(),        Toast.LENGTH_SHORT).show();

             }
         })
         .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int whichButton) {                     
             }
         })
         .create();     
}

}

alertstedit.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView 
    android:id="@+id/username_view"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:text="@string/alert_dialog_username"
    android:gravity="left"
    android:textAppearance="?android:attr/textAppearanceMedium" />            
<EditText
    android:id="@+id/username_edit"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:scrollHorizontally="true"
    android:autoText="false"
    android:capitalize="none"
    android:gravity="fill_horizontal"
    android:textAppearance="?android:attr/textAppearanceMedium" />        
</LinearLayout>

1 Ответ

4 голосов
/ 19 июня 2010

Ваш вызов findViewByID возвращает значение NULL, поскольку он вызывается в вашей активности, и этот макет не содержит элемент с таким идентификатором. Поскольку вы не проверяете, имеет ли edittext значение NULL, вызов функции getText () закрывается.

Если вы замените:

EditText edittext = (EditText) findViewById(R.id.username_edit);

по:

EditText edittext = (EditText) textEntryView.findViewById(R.id.username_edit);

Вы выполняете поиск в textEntryView (который вы использовали для создания диалога), и это работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...