Android—开发自学历程(1)-Activity(3)-第一个小程序

本人是个行动派,光看理论固然重要,但我觉得实践是最重要的。更不要说编程了。所以我在了解完了Activity后,就开始写我的第一个小程序。而且这里还是有学习新内容的哦!


这里我们就做一个简单的猜谜应用,先看下效果图。
效果图


然后是具体实现代码。
首先是布局文件:

<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:id="@+id/riddle_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24dp"
        android:text="@string/riddle_label" />

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <EditText 
            android:id="@+id/answer_editext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/answer_label"/>
        <Button 
            android:id="@+id/send_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/send_label"/>
        <Button 
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next_label"/>
    </LinearLayout>

</LinearLayout>

MainActivity.java

package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TextView mRiddleText = null;
    private EditText mAnswerText = null;
    private Button mSendButton = null;
    private Button mNextButton = null;

    //初始化组件
    private void initWidget() {
        mRiddleText = (TextView) findViewById(R.id.riddle_title);
        mAnswerText = (EditText) findViewById(R.id.answer_editext);
        mSendButton = (Button) findViewById(R.id.send_button);
        mNextButton = (Button) findViewById(R.id.next_button);
    }
    /**谜语题目
     * 问题ID
     * 问题比较
     */
    private int mCurrentIndex = 0;
    private InsClass[] mTitle = new InsClass[] {
        new InsClass(R.string.title_one, "merchandise"),
        new InsClass(R.string.title_two, "Q"),
        new InsClass(R.string.title_three, "horse")
    };

    private void updataRiddle() {
        int riddle = mTitle[mCurrentIndex].getTitle();
        mRiddleText.setText(riddle);
    }
    private void compareQuestion() {
        String trueAnswer = mTitle[mCurrentIndex].getAnswer();
        if(TextUtils.isEmpty(mAnswerText.getText()) ){
            Toast.makeText(MainActivity.this, "答案不能为空!请输入 ", Toast.LENGTH_LONG).show();
        } else {
            if (mAnswerText.getText().toString().equals(trueAnswer)) {
                Toast.makeText(MainActivity.this, "答案正确! ", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MainActivity.this, "答案错误! ", Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initWidget();
        mRiddleText.setText(mTitle[mCurrentIndex].getTitle());
        mRiddleText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mTitle.length;
                updataRiddle();
            }
        });
        mSendButton.setOnClickListener(new mySendButtonListener());
        mNextButton.setOnClickListener(new myNextButtonListener());
    }

    private class mySendButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            compareQuestion();
        }

    }
    private class myNextButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex + 1) % mTitle.length;
            updataRiddle();
        }

    }

}

工具类代码

package com.example.helloandroid;

public class InsClass {
    private int mTitle = 0;
    private String mAnswer = null;

    public InsClass(int titleOne, String answer) {
        mTitle = titleOne;
        mAnswer = answer;
    }

    public int getTitle() {
        return mTitle;
    }

    public void setTitle(int title) {
        mTitle = title;
    }

    public String getAnswer() {
        return mAnswer;
    }

    public void setAnswer(String answer) {
        mAnswer = answer;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值