Android开发初体验

Android开发初体验

前言
我们已经学习过Android开发的入门级程序 现在我们近一步学习Android,来跟着我学习吧!!!

步骤

一. 打开Android studio,新建一个工程。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
二.进行布局。
1.首先我们需要以下组件
一个垂直LinearLayout组件
一个TextView组件
一个水平LinearLayout组件
两个Button组件
2.在XML文件中定义组件,把以下代码删掉,重新设置。

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

3.更改为以下代码

<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="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="true_button" />
        <Button
            android:id="@+id/flase_button"
            android:layout_width ="wrap_content"
            android:layout_height="wrap_content"
            android:text="/false_button" />

    </LinearLayout>

</LinearLayout>

4.这样就完成布局了,返回手机界面就可以看到设置好的布局
在这里插入图片描述
三.创建字符串资源,在string.xml中添加。
在这里插入图片描述
五.打开java的文件,近一步添加成员变量。在这里插入图片描述
六.继续添加代码,引用组件。
在这串代码下,添加代码
在这里插入图片描述

代码如下:

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!
            }
        });

七.再次添加一个组件button,代码如下:

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NEXT  >" />

在这个地方添加
在这里插入图片描述
八.创建新类
点击java,右键new一个java class,命名为Question,点击ok。
在这里插入图片描述
九.在这个文件里添加代码,代码如下:

private int mTextResId;
private  boolean mAnswerTrue;
public Question(int mTextResId,boolean mAnswerTrue){
        mTextResId=mTextResId;
        mAnswerTrue=mAnswerTrue;

然后配置Android studio识别成员变量的前缀
在这里插入图片描述
十.生成getter方法与setter方法,在新建java文件下,代码如下

public  int getTextResId()  {
        return mTextResId;
    }
    public  void setTextResId(int textResId){
        mTextResId=textResId;
    }
    public boolean isAnswerTrue(){
        return mAnswerTrue;
    }
    public void setAnswerTrue(boolean answerTrue){
        mAnswerTrue=answerTrue;
    }

在这里插入图片描述
十一.连接模拟器
在这里插入图片描述

十二.连接手机,实现真机。
在这里插入图片描述

总结

希望我们能一起进步,感受编程的快乐。有错误请指出,谢谢~

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值