本人是个行动派,光看理论固然重要,但我觉得实践是最重要的。更不要说编程了。所以我在了解完了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;
}
}