Android开发初体验(Android Studio4.1.1版本)
一.搭建工程
二.开发初体验
(1).点击MainActivity.java 。注意事项如图及代码 ;如图
(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();
}
});
}
}
👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆
(2).activity_main.xml 如图,
(activity_main.xml代码) 如下图,
👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="25dp"
android:text="@string/question_text"
/>
<!--android:padding="24dp"内边距-->
<!--android:text="@string/question_text"对字符串资源的引用-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="ExtraText">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"
tools:ignore="ButtonStyle"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
tools:ignore="ButtonStyle"
android:layout_marginLeft="10dp"/>
</LinearLayout>
👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆
(3).strings.xml;如图,
(strings.xml 代码)如下图:
👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇
GeoQuiz
Constantinople is the largest city in Turkey.
TRUE
FALSE
Incorrect!
correct
👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆
三. 虚拟机上运行
1.如果点击虚拟机没有反应就重新安装虚拟机
四. 真实机和虚拟机一样,想要手机出现相应的文字,英文和数字等就到Android里面修改虚拟机也是一样。