Android开发初体验
前言
一、搭建工程
二、开发初体验
三、应用的运行
1.在模拟器上运行和2.在真实机上运行
前言
通过几天学习,慢慢掌握了一些Android Studion知识,下面就是我学习的第二个实验(仅供参考):
一、搭建工程
1.启动Android Studion程序,点击File创建新的工程。
2.进入下面的界面,然后选择“Emptp Activity”的空白项目,点击Next继续。
3.进入下面的界面工程名和包名可以改,Laguage这里建议选择Java,然后点击Finish,
二、开发初体验
1.点击 MainActivity.java 页面,进行程序的编写。
2.点击进入strings.xml页面,进行程序的编写。
3.点击进入activity_main.xml页面,进行程序的编写
附代码如下(示例):
这是MainActivity.java的代码
package com.example.geoquiz;
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; //添加两个按钮成员变量,m开头表示menmber成员变量
private Button mFalseButton;
@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 view) {
//创建提示消息
Toast.makeText(MainActivity.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 view) {
Toast.makeText(MainActivity.this, R.string.correct_toast,
Toast.LENGTH_SHORT).show();
}
});
}
}
这是strings.xml的代码
<resources>
<string name="app_name">GeoQuiz</string>
<string name="question_text">
Constantinople is the largest city in Turkey.
</string>
<string name="true_button">TRUE</string>
<string name="false_button">FALSE</string>
<string name="incorrect_toast">Correct1!</string>
<string name="correct_toast">Correct0!</string>
</resources>
这是activity_main.xm的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.geoquiz.MainActivity">
<!--android:orientation="vertical"垂直布局-->
<!--android:padding="24dp"内边距-->
<!--android:text="@string/question_text"对字符串资源的引用-->
<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"
android:layout_marginRight="8dp"
/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
android:layout_marginLeft="8dp"
/>
</LinearLayout>
<!--android:orientation="horizontal"水平布局-->
</LinearLayout>
三、应用的运行
1.如果不会找虚拟机请看我的第二篇文章,这就是虚拟机运行后的结果:
2.真实机运行后的结果: