Danh mục

Bắt Đầu Với Android (P.5)

Số trang: 50      Loại file: pdf      Dung lượng: 1.38 MB      Lượt xem: 25      Lượt tải: 0    
Hoai.2512

Phí tải xuống: 17,000 VND Tải xuống file đầy đủ (50 trang) 0
Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu bắt đầu với android (p.5), công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Bắt Đầu Với Android (P.5)Murphy_2419-8C19.fm Page 177 Wednesday, April 22, 2009 5:39 PM CHAPTER 19 ■ WORKING WITH RESOURCES 177 You can access these the same as with plain strings, with the exception that the result of the getString() call is really an object supporting the android.text.Spanned interface: ((TextView)findViewById(R.layout.another_label)) .setText(getString(R.string.laughs)); Styled Formats Where styled text gets tricky is with styled string formats, as String.format() works on String objects, not Spanned objects with formatting instructions. If you really want to have styled string formats, here is the workaround: 1. Entity-escape the angle brackets in the string resource (e.g., this is %1$s). 2. Retrieve the string resource as normal, though it will not be styled at this point (e.g., getString(R.string.funky_format)). 3. Generate the format results, being sure to escape any string values you substitute in, in case they contain angle brackets or ampersands. String.format(getString(R.string.funky_format), TextUtils.htmlEncode(strName)); 4. Convert the entity-escaped HTML into a Spanned object via Html.fromHtml(). someTextView.setText(Html .fromHtml(resultFromStringFormat)); To see this in action, let’s look at the Resources/Strings demo, which can be found in the Source Code area of http://apress.com. Here is the layout file: Murphy_2419-8C19.fm Page 178 Wednesday, April 22, 2009 5:39 PM178 CHAPTER 19 ■ WORKING WITH RESOURCES As you can see, it is just a button, a field, and a label. The intent is for somebody to enter their name in the field, then click the button to cause the label to be updated with a formatted message containing their name. The Button in the layout file references a string resource (@string/btn_name), so we need a string resource file (res/values/strings.xml): StringsDemo Name: My name is %1$s The app_name resource is automatically created by the activityCreator script. The btn_ name string is the caption of the Button, while our styled string format is in funky_format. Finally, to hook all this together, we need a pinch of Java: package com.commonsware.android.resources; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.text.Html; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class StringsDemo extends Activity { EditText name; TextView result; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); name=(EditText)findViewById(R.id.name); result=(TextView)findViewById(R.id.result); Button btn=(Button)findViewById(R.id.format);Murphy_2419-8C19.fm Page 179 Wednesday, April 22, 2009 5:39 PM CHAPTER 19 ■ WORKING WITH RESOURCES 179 btn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { applyFormat(); } }); } private void applyFormat() { String format=getString(R.string.funky_format); String simpleResult=String.format(format, TextUtils.htmlEncode(name.getText().toString())); result.setText(Html.fromHtml(simpleResult)); } } The string resource manipulation can be found in applyFormat(), which is called when the button is clicked. First, we get our format via getString()—something we could have done at onCreate() time for efficiency. Next, we format the value in the field using this format, getting a String back, since the string resource is in entity-encoded HTML. Note the use of TextUtils. h ...

Tài liệu được xem nhiều: