开发者

Is there a good code example for accessing the elements in a layout after the user has changed them?

I am currently learning how to program Java in android. I read various artices on how the layouts work with xml. There are some good examples that show this.. However, I have not seen any examples that show you how to access the elements from the layout once they are changed on the screen..

Does anybody know of a good code example to show the onClick Event for buttons.. and the syntax to enter the value that is typed in by the user in and EditText field..

any help is appr开发者_如何学Goeciated..

Regards, Jeff


Here's a simple example.

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
            android:id="@+id/edit" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />  
        <Button 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/button"
            android:onClick="getText" />
</LinearLayout>

The Button has an onClick method called getText, and the Edit text has an ID of edit. In the Activity that uses this Layout, you would write the following code:

public void getText(View view) {
    EditText editText = (EditText)findViewById(R.id.edit);
    String text = editText.getText().toString();
   // do something with the text
}

Alternatively, if your Button XML has an ID but no onClick, like this:

<Button 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/button"
    android:id="@+id/button" />

Then you would write the onClick handler in your Activity like this:

Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new onClickListener() {
    EditText editText = (EditText)findViewById(R.id.edit);
    String text = editText.getText().toString();
   // do something with the text    
});


For a button:

Button button = (Button) findViewById(R.id.my_button);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //do something
    }
});

For a TextView/EditText/ or other word-based layout item, you can implement TextWatcher and override the required methods. For example:

EditText myEditText = (EditText) findViewById(R.id.my_edittext);

@Override
public void afterTextChanged(Editable s) {
    if (s == myEditText) {
        //do something
    }
}


In the XML, important elements should be given an android:id, such as android:id="@+id/myTextField". The @<resource_type>/<resource_name> syntax is very important and is used commonly, for example:

  • @drawable/myPicture
  • @string/someText
  • @id/myTextField

These refer to resources in your /res/ folder. You can in fact create ID resources in an XML in res/values, but it's much more common and easier to do @+id/name as short hand for creating and using and ID right then and there in your layout XML, since IDs have no data associated with them like strings or images do.

  • @+id/myTextField

Now, in the Java itself, you can refer to any resource by IDs that are auto-generated in R.java:

  • R.drawable.myPicture
  • R.string.someText
  • R.id.myTextField

So when you want to access a text field, you use the R.id.myTextField and pass it to Activity.findViewById() or View.findViewById() to find a view inside that view (Activity.findViewById() is just a shortcut for calling the root view's findViewById).

For an example, lets say we are in onCreate or in a callback from the android:onClick property or a subclassed view:

EditText editText = (EditText) this.findViewById(R.id.myTextField);
CharSequence text = editText.getText();

Or somewhere not inside a View or Activity, but you have the View v object you want a child view of:

EditText editText = (EditText) v.findViewById(R.id.myTextField);
CharSequence text = editText.getText();

That's all there is to it. Now once you've got your EditText you can use getText to get a CharSequence (Editable), which has the same methods as a String:

Here's a more complete example of it in onButtonClick (if you give the button android:onClick="onButtonClick" android:id="@+id/buttonId" you can put this right into your Activity):

public void onButtonClick(View v) {
    switch (v.getId()) {
    case R.id.buttonId:
        EditText editText = (EditText) this.findViewById(R.id.myTextField);
        CharSequence text = editText.getText();
        // use the text at the time the user clicked the button
        break;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜