从零开始学Android,看了一天的视频的收获,了解了一些基本布局和一些基本控件的使用。
TextView
EditText
Button
RadioButton
CheckBox
了解了Activity生命周期的一些基本概念。
onCreate()
onStart()
onRestart()
onResume()
onPause()
onStop()
onDestroy()
代码根据视频纯手敲,通过测试,记录下来!
MainActivity.java
package com.example.helloworld;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button myButton=null;
private TextView myTextView=null;
private EditText factorOne=null;
private EditText factorTwo=null;
private RadioGroup genderGroup=null;
private RadioButton femaleButton=null;
private RadioButton maleButton=null;
private CheckBox swimBox = null;
private CheckBox runBox = null;
private CheckBox readBox = null;
private Button sendMsgButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("MainActivity------->onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
factorOne = (EditText)findViewById(R.id.factorOne);
factorTwo = (EditText)findViewById(R.id.factorTwo);
myTextView = (TextView)findViewById(R.id.myTextID);
myButton = (Button)findViewById(R.id.myButtonID);
myTextView.setText(R.string.textView);
myButton.setText("等于");
myButton.setOnClickListener( new MyButtonListener());
genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
maleButton = (RadioButton)findViewById(R.id.maleButton);
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(femaleButton.getId() == checkedId)
{
System.out.println("female");
Toast.makeText(MainActivity.this, "female~~~", Toast.LENGTH_SHORT).show();
}
else if(maleButton.getId() == checkedId)
{
System.out.println("male");
Toast.makeText(MainActivity.this, "male~~~", Toast.LENGTH_SHORT).show();
}
}
});
swimBox = (CheckBox)findViewById(R.id.swim);
runBox = (CheckBox)findViewById(R.id.run);
readBox = (CheckBox)findViewById(R.id.read);
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("swim is checded");
}
else
{
System.out.println("swim is unchecded");
}
}
});
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("run is checded");
}
else
{
System.out.println("run is unchecded");
}
}
});
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("read is checded");
}
else
{
System.out.println("read is unchecded");
}
}
});
sendMsgButton = (Button)findViewById(R.id.sendMsgButtonID);
sendMsgButton.setOnClickListener(new sendMsgButtonListener());
}
//当客户点击NENU按钮的时候,调用该方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(0,1,1,"退出");
menu.add(0,2,2,"关于");
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//当客户点击菜单当中的某一个选项时,会调用该方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId() == 1)
{
finish();
}
return super.onOptionsItemSelected(item);
}
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String factorOneStr = factorOne.getText().toString();
String factorTwoStr = factorTwo.getText().toString();
//生成一个Intent对象;
Intent intent = new Intent();
intent.putExtra("one", factorOneStr);
intent.putExtra("two", factorTwoStr);
intent.setClass(MainActivity.this, OtherActivity.class);
MainActivity.this.startActivity(intent);
}
}
//调用发送短信的界面
class sendMsgButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("smsto:13500001111");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("MainActivity------->onDestroy");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("MainActivity------->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("MainActivity------->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("MainActivity------->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("MainActivity------->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("MainActivity------->onStop");
super.onStop();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/factorOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myTextID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/factorTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/myButtonID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
/>
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
/>
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
/>
</RadioGroup>
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="swim"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="run"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="read"
/>
<Button
android:id="@+id/sendMsgButtonID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="send Message"
/>
</LinearLayout>
OtherActivity.java
package com.example.helloworld;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
public class OtherActivity extends Activity{
private TextView myOtherTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
System.out.println("OtherActivity------->onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Intent intent = getIntent();
String factorOneStr = intent.getStringExtra("one");
String factorTwoStr = intent.getStringExtra("two");
//JAVA中String转int的方法
int factorOneInt = Integer.parseInt(factorOneStr);
int factorTwoInt = Integer.parseInt(factorTwoStr);
int value = factorOneInt * factorTwoInt;
myOtherTextView = (TextView)findViewById(R.id.myOtherTextID);
myOtherTextView.setText(value + "");
// myOtherTextView.setText(R.string.other);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("OtherActivity------->onDestroy");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("OtherActivity------->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("OtherActivity------->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("OtherActivity------->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("OtherActivity------->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("OtherActivity------->onStop");
super.onStop();
}
}
other.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/myOtherTextID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>