基于Android的背单词软件设计与实现计算机毕业论文(8)

2019-03-16 09:59

import android.os.Bundle; import android.view.View; import android.widget.Button; import java.util.Date; public class NowRedux extends Activity implements View.OnClickListener { Button btn;

@Override

public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

btn=(Button)findViewById(R.id.button);btn.setOnClickListener(this);updateTime();

}

public void onClick(View view) { updateTime(); }

private void updateTime() { btn.setText(new Date().toString()); } }

The first difference is that rather than setting the content view to be a view we created in Java code, we set it to reference the XML layout (setContentView(R.layout.main)). The R.java source file will be updated when we rebuild this project to include a reference to our layout file (stored as main.xml in our project’s res/layout directory). The other difference is that we need to get our hands on our Button instance, for which we use the findViewById() call. Since we identified our button as @+id/button, we can reference the button’s identifier as R.id.button. Now, with the Button instance in hand, we can set the callback and set the label as needed. As you can see in Figure 5-1, the results look the same as with the original Now demo. 32

Figure 5-1. The NowRedux sample activity

Employing Basic Widgets Every GUI toolkit has some basic widgets: fields, labels, buttons, etc. Android’s toolkit is no different in scope, and the basic widgets will provide a good introduction as to how widgets work in Android activities. Assigning Labels The simplest widget is the label, referred to in Android as a TextView. Like in most GUI toolkits, labels are bits of text not editable directly by users. Typically, they are used to identify adjacent widgets (e.g., a ―Name:‖ label before a field where one fills in a name). In Java, you can create a label by creating a TextView instance. More commonly, 33

though, you will create labels in XML layout files by adding a TextView element to the layout, with an android:text property to set the value of the label itself. If you need to swap labels based on certain criteria, such as internationalization, you may wish to use a resource reference in the XML instead, as will be described in Chapter 9. TextView has numerous other properties of relevance for labels, such as:

? android:typeface to set the typeface to use for the label (e.g., monospace) ? android:textStyle to indicate that the typeface should be made bold (bold), italic (italic),or bold and italic (bold_italic) ? android:textColor to set the color of the label’s text, in RGB hex format (e.g., #FF0000 for red) For example, in the Basic/Label project, you will find the following layout file:

34

Button, Button, Who’s Got the Button? We’ve already seen the use of the Button widget in Chapters 4 and 5. As it turns out, Button is a subclass of TextView, so everything discussed in the preceding section in terms of formatting the face of the button still holds. Fleeting Images Android has two widgets to help you embed images in your activities: ImageView and ImageButton. As the names suggest, they are image-based analogues to TextView and Button, respectively. Each widget takes an android:src attribute (in an XML layout) to specify what picture to use. These usually reference a drawable resource, described in greater detail in the chapter on resources. You can also set the image content based on a Uri from a content provider via setImageURI().

ImageButton, a subclass of ImageView, mixes in the standard Button behaviors, for responding to clicks and whatnot.

For example, take a peek at the main.xml layout from the Basic/ImageView sample project which is found along with all other code samples at http://apress.com:

The result, just using the code-generated activity, is shown in Figure 6-2. 35

Figure 6-2. The ImageViewDemo sample application Fields of Green. Or Other Colors. Along with buttons and labels, fields are the third ―anchor‖ of most GUI toolkits. In Android, they are implemented via the EditText widget, which is a subclass of the TextView used for labels.

Along with the standard TextView properties (e.g., android:textStyle), EditText has many others that will be useful for you in constructing fields, including: ? android:autoText, to control if the field should provide automatic spelling assistance ? android:capitalize, to control if the field should automatically capitalize the first letter of entered text (e.g., first name, city) ? android:digits, to configure the field to accept only certain digits ? android:singleLine, to control if the field is for single-line input or multiple-line input (e.g., does move you to the next widget or add a newline?) Beyond those, you can configure fields to use specialized input methods, such as android:numeric for numeric-only input, android:password for shrouded password input, 36


基于Android的背单词软件设计与实现计算机毕业论文(8).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:新校区硬化监理大纲

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: