CLASS
MainActivity
*********************************************************************************
package com.nanolaser;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private final String TAG="Main Activity";
@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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG, "onStart");
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i(TAG, "onResume");
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i(TAG, "onPause");
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i(TAG, "onStop");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG, "onDestroy");
}
public void buttonClick (View v){
Log.i(TAG, "Button Clicked");
final TextView textView=(TextView) findViewById(R.id.name);
String text=textView.getText().toString();
if (text.contains("world"))
{
textView.setText("Hello, Leize");
}
else
{
textView.setText("Hello, world");
}
}
}
*******************************************************************************
NewActivity
*******************************************************************************
package com.nanolaser;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class NewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.new_activity);
}
public void addComment(View v)
{
String name, comment, other;
EditText nameIn=(EditText) findViewById (R.id.nameIn);
EditText commentIn=(EditText) findViewById (R.id.commentIn);
EditText otherIn=(EditText) findViewById (R.id.otherIn);
name=nameIn.getText().toString();
comment=commentIn.getText().toString();
other=otherIn.getText().toString();
TextView nameOut=(TextView)findViewById (R.id.name);
TextView commentOut=(TextView)findViewById (R.id.comment);
TextView otherOut=(TextView)findViewById (R.id.other);
nameOut.setText(name);
commentOut.setText(comment);
otherOut.setText(other);
}
}
********************************************************************************
layout
activity_main
********************************************************************************
<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:gravity="center"
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/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:text="@string/button_string"
android:onClick="buttonClick"/>
</RelativeLayout>
*********************************************************************************
new_activity
********************************************************************************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="5dp" >
<EditText
android:id="@+id/nameIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/name"
android:inputType="text" >
</EditText>
<EditText
android:id="@+id/commentIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="@string/comment"
android:inputType="text" />
<EditText
android:id="@+id/otherIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/other"
android:inputType="text" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addcomment"
android:onClick="addComment"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/blank"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/blank"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/blank"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
*******************************************************************************
layout-land
activity_main
*******************************************************************************
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
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/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="@string/button_string" />
</LinearLayout>
********************************************************************************
layout-port
activity_main
*******************************************************************************
<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:gravity="center"
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/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:text="@string/button_string"
android:onClick="buttonClick"/>
</RelativeLayout>
**********************************************************************************
AndroidManifest
********************************************************************************
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nanolaser"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.nanolaser.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>
</application>
</manifest>
No comments:
Post a Comment