Android开发初体验

本次实验将带你开发第一个应用,并借此学习一些Android基本概念以及构成应用的UI组件。马上要开发的应用名叫GeoQuiz,它能给出一道地理知识问题。用户点击TRUE或FALSE按钮来回答屏幕上的问题,GeoQuiz即时作出反馈。
先进行创建项目
在这里插入图片描述
这里记得选择Java语言
在这里插入图片描述
接下来我们要修改默认组件,先修改activity_main.xml,将下列代码输入到activity_main.xml中。
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/flase_button"
android:layout_width =“wrap_content”
android:layout_height=“wrap_content”
android:text="@string/false_button" />
/LinearLayout>
/LinearLayout>
在这里插入图片描述
然后修改string.xml,将下列代码输入到string.xml中
resources>
string name=“app_name”>GeoQuiz
string name=“question_text”>Canberra is the capital of Australia.
string name=“true_button”>True
string name=“false_button”>False
string name=“correct_toast”>Correct!
string name=“incorrect_toast”>Incorrect!

/resources>
在这里插入图片描述
最后修改MainActivity.java,将下列代码输入到MainActivity.java中
package com.example.genquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Button mTrueButton;
private Button mFlaseButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Toast.makeText(MainActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
            //Does nothing yet,but soon!
        }
        });
    mFlaseButton = (Button) findViewById(R.id.flase_button);
    mFlaseButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Toast.makeText(MainActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
            //Does nothing yet,but soon!
        }
    });
}

}
在这里插入图片描述
最后在模拟机上运行
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值