笨笨安卓学习的第一天

来源:互联网 发布:win10无法开启网络发现 编辑:程序博客网 时间:2024/06/05 11:58

        之前总说自己在学安卓,今天算真正开始学,当然,也感谢之前混的日子总数稍微了解了点点概念。

        希望通过记日志,既巩固知识,也可以整理学习思路

       

MainActivity.java
package com.example.activitylife;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private Button btnStartAty1;private TextView tvOut;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tvOut=(TextView) findViewById(R.id.tvOut);btnStartAty1=(Button) findViewById(R.id.btnStartAty1);btnStartAty1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent i=new  Intent(MainActivity.this,Aty1.class);//i.putExtra("txt", "Hello Aty1");Bundle data=new Bundle();data.putString("txt", "Hello Aty1");i.putExtras(data);//startActivity(i);startActivityForResult(i, 0);}});System.out.println("onCreat");}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubString result=data.getStringExtra("result");tvOut.setText(result);super.onActivityResult(requestCode, resultCode, data);}@Overridepublic 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;}@Overrideprotected void onStart() {// TODO Auto-generated method stubsuper.onStart();System.out.println("onStart");}@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.onRestart();}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();System.out.println("onRestart");}@Overrideprotected void onPause() {// TODOuto-generated method stubsuper.onPause();System.out.println("onPause");}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();System.out.println("onStop");}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("onDestroy");}@Overridepublic 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);}}

</pre><pre name="code" class="java"><pre name="code" class="html">Aty1.java

package com.example.activitylife;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class Aty1 extends Activity {private Button btnclose;private TextView tvout;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.aty1);btnclose=(Button) findViewById(R.id.btnclose);btnclose.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent i=new Intent();i.putExtra("result", "Hello Mainactivity");setResult(0, i);finish();}});tvout=(TextView) findViewById(R.id.tvout);//tvout.setText(getIntent().getStringExtra("txt"));Bundle data=getIntent().getExtras();String txt=data.getString("txt");tvout.setText(txt);}}

activity_main.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="com.example.activitylife.MainActivity" >    <TextView        android:id="@+id/tvOut"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button        android:id="@+id/btnStartAty1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/textView1"        android:layout_marginLeft="38dp"        android:layout_marginTop="76dp"        android:text="StartAty1" /></RelativeLayout>

<pre name="code" class="html">aty1.xml

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <CheckedTextView        android:id="@+id/tvout"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Aty1" />    <Button        android:id="@+id/btnclose"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="close" /></LinearLayout>

manifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.activitylife"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="17"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name="Aty1"></activity>    </application></manifest>

果然本本


































1 0
原创粉丝点击