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
Android APP--建立简单的交互界面_第1张图片

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

布局文件:

                

资源文件:

   
    GeoQuiz            Constantinople is the largest city in Turkey.        True    False    Correct!    Incorrect!    Settings

主程序:

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 APP--建立简单的交互界面_第2张图片


更多相关文章

  1. Android中编码实现软件界面
  2. android gridview按钮边框和定制点击颜色
  3. android 如何在对话框中获取edittext中的数据
  4. android原生音乐播放器界面字体显示不全
  5. android 按钮Button单击背景切换
  6. android子线程中刷新界面控件
  7. Android 动态切换底部tab按钮
  8. Android 实现在Java代码中修改UI界面,并修改界面
  9. Android 的 Button 按钮实现的两种方式

随机推荐

  1. Android Studio 安装后无法运行
  2. android学习任务
  3. Android网络状态相关
  4. Android实现View的任意拖动
  5. [zz]android框架示意图
  6. Mac通过Android aapt解析apk包名、版本号
  7. Volley的基本使用
  8. Android Jni开发环境搭建完整版
  9. EditText点击隐藏hint
  10. Android中Activity启动过程源码阅读笔记: