Android APP--建立简单的交互界面
2016-10-19 09:14
211 查看
Widgets are the building blocks you use to compose a user interface. A widget can show test or graphics, interact with the user, or arrange other widgets on the screen. Buttons, text input controls, and checkboxes are all types of widgets.
The interface for QuizActivity requires five widgets:
a vertical LinearLayout
a TextView
a horizontal LinearLayout
two Buttons
To get the activity its user interface, you call the following Activity method:
public void setContentView(int layoutResD)
This method inflates a layout and puts it on screen. When a layout is inflated, each widget in the layout file is instantiated as defined by its attributes.
In an activity, you can get a reference to an inflated widget by calling the following Activity method:
public View findViewById(int id)
This method accepts a resource ID of a widget and returns a View object.
When your application is waiting for a specific event, we say that it is "listening for" that event. The object that you create to respond an an event is called a listener, and the listener implements a lestener interface for that event.
This listener is implementd as an anonymous inner class. The syntax is a little tricky, but it helps to remember that everything within the outermost set of parentheses is passed into setOnClickListener(OnClickListener).
To create a toast, you call the following method from the Toast class:
public static Toast makeText(Context context, int resId, int duration)
The Context parameter is typically an instance of Activity(Activity is a subclass of Context).
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"/>
</LinearLayout>
</LinearLayout>
资源文件:
主程序:
运行结果:
当点击TRUE按钮时,弹出对话框Incorrect!
当点击FLASE按钮时,弹出对话框Correct!
The interface for QuizActivity requires five widgets:
a vertical LinearLayout
a TextView
a horizontal LinearLayout
two Buttons
To get the activity its user interface, you call the following Activity method:
public void setContentView(int layoutResD)
This method inflates a layout and puts it on screen. When a layout is inflated, each widget in the layout file is instantiated as defined by its attributes.
In an activity, you can get a reference to an inflated widget by calling the following Activity method:
public View findViewById(int id)
This method accepts a resource ID of a widget and returns a View object.
When your application is waiting for a specific event, we say that it is "listening for" that event. The object that you create to respond an an event is called a listener, and the listener implements a lestener interface for that event.
This listener is implementd as an anonymous inner class. The syntax is a little tricky, but it helps to remember that everything within the outermost set of parentheses is passed into setOnClickListener(OnClickListener).
To create a toast, you call the following method from the Toast class:
public static Toast makeText(Context context, int resId, int duration)
The Context parameter is typically an instance of Activity(Activity is a subclass of Context).
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"/>
</LinearLayout>
</LinearLayout>
资源文件:
<resources> <string name="app_name">GeoQuiz</string> <string name="question_text"> Constantinople is the largest city in Turkey. &l 4000 t;/string> <string name="true_button">True</string> <string name="false_button">False</string> <string name="correct_toast">Correct!</string> <string name="incorrect_toast">Incorrect!</string> <string name="action_settings">Settings</string> </resources>
主程序:
package com.android.testrecord; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class QuizActivity extends AppCompatActivity { private Button mTrueButton; private Button mFalseButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz); mTrueButton = (Button) findViewById(R.id.true_button); mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Does nothing yet, but soon! Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show(); } }); mFalseButton = (Button) findViewById(R.id.false_button); mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Does nothing yet, but soon! Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show(); } }); } }
运行结果:
当点击TRUE按钮时,弹出对话框Incorrect!
当点击FLASE按钮时,弹出对话框Correct!
相关文章推荐
- Android开发第1-3课:建立一个简单的用户交互界面
- Android 计算器解析(一): 建立简单界面
- Android上实现一个简单的天气预报APP(七) 切换到新的界面(选择城市界面)
- Android简单扫码APP设计——从欢迎、登陆到主功能界面
- 【Android 简单通信组件学习】ContentProvide 唯一与所有app交互的组件
- Android上实现一个简单的天气预报APP(六) 更新界面数据
- Android App启动图启动界面(Splash)的简单实现
- Android开发学习笔记(六):最简单的办法实现APP欢迎界面
- android中不同app间数据交互(1、简单一次性数据交互)
- android多线程与界面交互以及runOnUiThread简单应用
- Android ui utils-简单实用的Android界面工具
- Android ui utils-简单实用的Android界面工具
- android学习—— 简单的实现 android 退出app 的功能, 非 restartPackage
- Android ui utils-简单实用的Android界面工具
- Android学习系列(5)--App布局初探之简单模型
- Android学习系列(5)--App布局初探之简单模型
- Android ui utils-简单实用的Android界面工具
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- Android ui utils-简单实用的Android界面工具
- Android:一个简单查询界面的实现