码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发初体验

时间:2017-09-17 22:03:54      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:string   next   cli   text   XML   按钮   warning   line   view   

第一个Androd应用-GeoQuiz,它能给出一道道地理问题,用户点击TRUE或FALSE按钮来回答屏幕上的问题,GeoQuiz及时做出反馈,首先我们来看看它完成后的样子

技术分享

1.首先我们创建一个Android项目

技术分享

接下来SDK的选择最低版本是4.4我选择的的是7.0,然后继续Next

技术分享

选择Empty Activity,继续Next

技术分享

命名activity的子类为Quizactivity,点击finsh.这样就完成了项目的创建

技术分享

 

2.接下来就是源码,在activity_quiz.xml添加组件

 

<?xml version="1.0" encoding="utf-8"?>
<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.bignerdranch.android.geoquiz.QuizActivity">
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_australia"/>

<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/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />


</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageButton android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_left"
android:text="@string/prev"/>

<Button
android:id="@+id/cheat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheat_button"/>

<ImageButton android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_right"
android:text="@string/next"/>
</LinearLayout>
</LinearLayout>

 然后是string.xml

<resources>
<string name="app_name">GeoQuiz</string>
<string name="question_australia">Canberra is the capital of Australia.</string>
<string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
<string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
<string name="question_africa">The source of the Nile River is in Egypt.</string>
<string name="question_americas">The Amazon River is the longest river in the Americas.</string>
<string name="question_asia">Lake Baikal is the world\‘s oldest and deepest freshwater lake.</string>

<string name="true_button">True</string>
<string name="false_button">False</string>
<string name="next_button">Next</string>
<string name="correct_toast">Correct!</string>
<string name="incorrect_toast">Incorrect!</string>

<string name="warning_text">Are you sure you want to do this?</string>
<string name="show_answer_button">SHOW ANSWER</string>
<string name="cheat_button">CHEAT!</string>
<string name="judgment_toast">Cheating is wrong.</string>

<string name="next">NEXT</string>
<string name="prev">PTEV</string>
</resources>

 和activity_cheat.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/warning_text"/>

<TextView
android:id="@+id/answer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
tools:text="Answer"/>

<Button
android:id="@+id/show_answer_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_answer_button"/>

</LinearLayout>
 3.创建数组对象,与Textview和button交互,对用户的回答做出反应 
 
 技术分享

 

 
 
  4.设置监听器@Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button); }
else {
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
}

 

Android开发初体验

标签:string   next   cli   text   XML   按钮   warning   line   view   

原文地址:http://www.cnblogs.com/haiou123/p/7537791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!