Android程序初体验
第一个程序的实现的最终功能是:
点击"正确"或者"错误"会得到一个是否正确的提示。
直接上效果图。
此次涉及代码编写的文件有4个:
package com.Kyleapp2;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity<QuizActivity> extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTrueButton = (Button)findViewById(R.id.true_button);
mFalseButton = (Button)findViewById(R.id.false_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Does nothing yet, but soon!
Toast.makeText
(MainActivity.this, R.string.correct_toast,
Toast.LENGTH_SHORT).show();
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Does nothing yet, but soon!
Toast.makeText(MainActivity.this, R.string.incorrect_toast,
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question" />
<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>
*/
public static final int abc_shareactionprovider_share_with_application=0x7f0a000b;
public static final int action_settings=0x7f0a000f;
public static final int app_name=0x7f0a000d;
public static final int correct_toast=0x7f0a0012;
public static final int false_button=0x7f0a0011;
public static final int incorrect_toast=0x7f0a0013;
public static final int question=0x7f0a000e;
public static final int true_button=0x7f0a0010;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">App2</string>
<string name="question">贵州是个美丽的地方?</string>
<string name="action_settings">Settings</string>
<string name="true_button">正确</string>
<string name="false_button">错误</string>
<string name="correct_toast">Correct!</string>
<string name="incorrect_toast">Incorrect!</string>
</resources>
布局预览
总结:
此过程,只需要关注以上四个文件。一下是它们各自的作用。
相当于我们的主程序文件,也就是"main.c"
显示的字符串信息在这里
程序界面控件在这里
这个不需要我们管,系统自动生成的