简体   繁体   中英

how to gettext from edittext in android?

I use the following code to take values from the textbox.But it's not working it gives the exception and force to close only.How to get the value of the textbox ?can anyone help me?

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();     
}

}

alertedit.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>

Your call to findViewByID returns null, because it's invoked on your Activity and that layout doesn't contain an item with that ID. Since you don't check if edittext is null, the call to getText() force closes.

If you replace:

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

by:

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

You search within textEntryView (which you used to create the dialog), and that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM