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;
}
十一.连接模拟器
十二.连接手机,实现真机。
总结
希望我们能一起进步,感受编程的快乐。有错误请指出,谢谢~