編輯:關於Android編程
首先看主布局文件main.xml:
[html]
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請輸入第一個數字:" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請輸入第二個數字:" />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />"
</LinearLayout>
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請輸入第一個數字:" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請輸入第二個數字:" />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />"
</LinearLayout>
下面來看主Activity文件,其內容如下:
[java]
package com.liuhaoyu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edit1 = (EditText)findViewById(R.id.editText1);
final EditText edit2 = (EditText)findViewById(R.id.editText2);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String num1 = edit1.getText().toString();
String num2 = edit2.getText().toString();
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("number1", num1);
bundle.putCharSequence("number2", num2);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
package com.liuhaoyu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edit1 = (EditText)findViewById(R.id.editText1);
final EditText edit2 = (EditText)findViewById(R.id.editText2);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String num1 = edit1.getText().toString();
String num2 = edit2.getText().toString();
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("number1", num1);
bundle.putCharSequence("number2", num2);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
在按鈕的監聽函數中,創建了一個Bundle對象,將用戶輸入的數值保存在其中,然後該Bundle做為Intent的附加參數傳遞給SecondActivity。
下面看SecondActivity的實現,其內容如下:
[java]
package com.liuhaoyu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
int sum;
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String num1 = bundle.getString("number1");
String num2 = bundle.getString("number2");
sum = Integer.parseInt(num1) + Integer.parseInt(num2);
TextView text = (TextView)findViewById(R.id.textView);
text.setText(num1 + " + " + num2 + " = " + String.valueOf(sum));
}
}
package com.liuhaoyu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
int sum;
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String num1 = bundle.getString("number1");
String num2 = bundle.getString("number2");
sum = Integer.parseInt(num1) + Integer.parseInt(num2);
TextView text = (TextView)findViewById(R.id.textView);
text.setText(num1 + " + " + num2 + " = " + String.valueOf(sum));
}
}
在onCreate函數中,首先通過getIntent()取得Intent,再通過Intent.getExtras()取得MainActivity傳遞過來的Bundle,由此即可以獲得傳遞過來的數值了。
SecondActivity的布局文件內容如下:
[html]
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:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:textSize="20dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />"
</LinearLayout>
<?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:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:textSize="20dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />"
</LinearLayout>
最後,在AndroidManifest.xml文件中加入SecondActivity的聲明:
[html]
<activity
android:icon="@drawable/ic_launcher"
android:name=".SecondActivity"
android:label="計算結果"
android:theme="@android:style/Theme.Dialog"
>
</activity>
去年的優酷HD版有過這樣一種菜單,如下圖:應用打開之後,先是三個弧形的三級菜單,點擊實體鍵menu之後,這三個菜單依次旋轉退出,再點擊實體鍵menu之後,一級菜單會旋轉進
博客好長時間沒有更新了,做個基礎總結,繼續之前的,溫故而知新!該系列為入門篇,大神可以繞道! 大家好,今天給大家詳
49.Android 各種SpanAndroid 各種Span 前言 SpannableStringBuilder URLSpan UnderlineSpan Typef
第8節 橫屏的播放界面在設備旋轉成橫屏的時候,視頻將自動進行全屏播放。8.1 播放器橫屏布局我們要為全屏播放界面設置一個新的布局,這個布局裡面只用來播放視頻,不需要顯示任