日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android使用Activity

發(fā)布時間:2024/9/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android使用Activity 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
第一個例子,顯示網(wǎng)址

首先創(chuàng)建工程

按照提示填入

我使用的是2.3版本,所以Min SDK Version填10

修改/res/layout/下main.xml文件

加入按鈕

對應(yīng)的程序文件如下:

View Code <Button android:layout_height="wrap_content"
android:layout_width
="wrap_content" android:text="@string/showurl"
android:id
="@+id/submit_to_net"></Button>

這樣就在頁面上繪制了一個按鈕,然后給按鈕添加事件,就是點擊后做什么

我的類信息是ActivityUse,這個類繼承自Activity

文件中程序如下:

View Code public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

submit_data_tonewactivity();

}

private void submit_data_tonewactivity() {
Button button_start_browser
= (Button) findViewById(R.id.submit_to_net);

button_start_browser.setOnClickListener(
new OnClickListener() {

public void onClick(View v) {
Uri myUri
= Uri.parse("http://www.baidu.com");
Intent openBrowseIntent
= new Intent(Intent.ACTION_VIEW, myUri);
startActivity(openBrowseIntent);
}
});


}

看這幾句

??? Uri myUri = Uri.parse("http://www.baidu.com");
????Intent openBrowseIntent = new Intent(Intent.ACTION_VIEW, myUri);
????startActivity(openBrowseIntent);

Intent是用于多個Activity之間進行跳轉(zhuǎn)的,Activity可以理解成web開發(fā)中的form.

程序調(diào)用瀏覽器,顯示網(wǎng)址。

第二個例子,跳轉(zhuǎn)頁面并提交數(shù)據(jù)

用剛才建好的工程

復(fù)制一個main.xml并且更名為welcome.xml

配置界面如下,并且在main.xml中加入文本框和登陸按鈕

welcome.xml中設(shè)置如下,需要對應(yīng)修改配置屬性?并在main.xml中加入如下設(shè)置?

View Code <?xml version="1.0" encoding="utf-8"?>
<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:text="請輸入..." android:layout_height="wrap_content"
android:layout_width
="match_parent" android:id="@+id/logintext"></EditText>
<Button android:layout_height="wrap_content"
android:layout_width
="wrap_content" android:text="@string/exit"
android:id
="@+id/btnexit"></Button>
</LinearLayout> View Code <EditText android:text="請輸入..." android:layout_height="wrap_content"
android:layout_width
="match_parent" android:id="@+id/name"></EditText>
<TextView android:text="TextView" android:id="@+id/result"
android:layout_width
="fill_parent" android:layout_height="wrap_content"></TextView>

<LinearLayout android:layout_height="wrap_content"
android:layout_width
="match_parent" android:id="@+id/linearLayout1">
<Button android:layout_height="wrap_content"
android:layout_width
="wrap_content" android:text="@string/showurl"
android:id
="@+id/submit_to_net"></Button>
<Button android:layout_height="wrap_content"
android:layout_width
="wrap_content" android:text="@string/show_login_name"
android:id
="@+id/show_login"></Button>
<Button android:layout_height="wrap_content"
android:layout_width
="wrap_content" android:text="@string/show_loginname"
android:id
="@+id/submit_to_showloginname"></Button>
</LinearLayout>

Activity,需要在AndroidManifest.xml中添加設(shè)置

View Code <activity android:name=".Welcome" android:label="welcome"></activity>

Welcome.java類

View Code public class Welcome extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
Bundle myBundleForGetName
= this.getIntent().getExtras();
String name
= myBundleForGetName.getString("key_name");
final EditText resultName = (EditText) findViewById(R.id.logintext);
resultName.setText(
"歡迎你" + name);
click_button();
}

private void click_button() {
final Button btnExit = (Button) findViewById(R.id.btnexit);
btnExit.setOnClickListener(btnexit_listener);
}
//返回到main頁
private Button.OnClickListener btnexit_listener = new Button.OnClickListener() {
public void onClick(View v) {
Intent main
= new Intent();
main.setClass(Welcome.
this, ActivityUse.class);
startActivity(main);
}
};

}
View Code private void submit_data_tonewactivity() {
final EditText inName = (EditText) findViewById(R.id.name);
final TextView result = (TextView) findViewById(R.id.result);
Button button_start_browser
= (Button) findViewById(R.id.submit_to_net);
Button button_login
= (Button) findViewById(R.id.show_login);
Button button_showLoginName
= (Button) findViewById(R.id.submit_to_showloginname);

button_start_browser.setOnClickListener(
new OnClickListener() {

public void onClick(View v) {
Uri myUri
= Uri.parse("http://www.baidu.com");
Intent openBrowseIntent
= new Intent(Intent.ACTION_VIEW, myUri);
startActivity(openBrowseIntent);
}
});

button_login.setOnClickListener(
new OnClickListener() {

public void onClick(View v) {
// 接受數(shù)據(jù)
Intent openWelcomeActivityIntent = new Intent();
Bundle myBundelForName
= new Bundle();
myBundelForName.putString(
"key_name", inName.getText()
.toString());
openWelcomeActivityIntent.putExtras(myBundelForName);
openWelcomeActivityIntent.setClass(ActivityUse.
this,
Welcome.
class);
startActivity(openWelcomeActivityIntent);
}
});

button_showLoginName.setOnClickListener(
new OnClickListener() {

public void onClick(View v) {
result.setText(inName.getText()
+ "歡迎您進入......");
}
});
}

注意這幾句

??? // 接受數(shù)據(jù)
????Intent openWelcomeActivityIntent = new Intent();
????Bundle myBundelForName = new Bundle();
????myBundelForName.putString("key_name", inName.getText()
??????.toString());
????openWelcomeActivityIntent.putExtras(myBundelForName);
????openWelcomeActivityIntent.setClass(ActivityUse.this,
??????Welcome.class);
????startActivity(openWelcomeActivityIntent);

新用到了Bundle,這個是在對個Activity之間傳遞數(shù)據(jù)用的,這個例子中將信息放入的方法是putExtras

在接受端,即Welcome.java中

? Bundle myBundleForGetName = this.getIntent().getExtras();
??String name = myBundleForGetName.getString("key_name");
??final EditText resultName = (EditText) findViewById(R.id.logintext);
??resultName.setText("歡迎你" + name);

接收數(shù)據(jù)并顯示,同樣的方法可以傳遞多個值

頁面樣例如下:

輸入111,點擊登陸



跳轉(zhuǎn)后的頁面如下:



點擊退出可以返回原頁面



第三個例子,跳轉(zhuǎn)頁面并且得到返回值

還是用剛才的工程

加入login.xml,和Login.java文件

并在AndroidManifest.xml指定?

View Code <application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ActivityUse"
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=".Welcome" android:label="welcome"></activity>
<activity android:name=".Login" android:label="login"></activity>

</application>

添加的登陸頁面效果

使用的是TableLayout



login.xml中信息?

View Code <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width="fill_parent"
android:layout_height
="fill_parent">
<TableLayout android:layout_height="wrap_content"
android:layout_width
="match_parent" android:id="@+id/tableLayout1">
<TableRow android:layout_height="wrap_content"
android:layout_width
="match_parent" android:id="@+id/tableRow1">
<TextView android:text="用戶名" android:id="@+id/txtName"
android:layout_width
="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/tname"
android:layout_width
="200px" android:layout_height="wrap_content"></EditText>
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width
="match_parent" android:id="@+id/tableRow1">
<TextView android:text="密 碼" android:id="@+id/txtPass"
android:layout_width
="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/tpass"
android:layout_width
="200px" android:layout_height="wrap_content"></EditText>
</TableRow>
</TableLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width
="match_parent" android:id="@+id/linearLayout1">
<Button android:text="登陸" android:id="@+id/btnLogin"
android:layout_width
="115px" android:layout_height="wrap_content"></Button>
<Button android:text="取消" android:id="@+id/btnExit"
android:layout_width
="115px" android:layout_height="wrap_content"></Button>
</LinearLayout>

</LinearLayout>

Login.java中信息?

View Code public class Login extends Activity {

/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

Button btnLogin
= (Button) findViewById(R.id.btnLogin);
Button btnExit
= (Button) findViewById(R.id.btnExit);

// 取值
final EditText etName = (EditText) this.findViewById(R.id.tname);
final EditText etPass = (EditText) this.findViewById(R.id.tpass);

btnLogin.setOnClickListener(
new OnClickListener() {

public void onClick(View v) {
Intent backIntent
= new Intent();
Bundle stringBundle
= new Bundle();
stringBundle.putString(
"loginName", etName.getText().toString());
stringBundle.putString(
"logPass", etPass.getText().toString());
backIntent.putExtras(stringBundle);
setResult(RESULT_OK, backIntent);
finish();
}
});

btnExit.setOnClickListener(
new OnClickListener() {

public void onClick(View v) {
Intent backIntent
= new Intent();
setResult(RESULT_CANCELED, backIntent);
finish();
}
});
}

}

修改main.xml,增加?同時修改ActivityUse.java,并且加入get_returnvalue();函數(shù)?接受返回值通過重寫

View Code <LinearLayout android:orientation="vertical"
android:layout_height
="wrap_content" android:layout_width="match_parent"
android:id
="@+id/linearLayout2">
<TextView android:text="返回的內(nèi)容顯示" android:id="@+id/textViewReturn"
android:layout_width
="fill_parent" android:layout_height="48px"></TextView>
<Button android:layout_height="wrap_content"
android:layout_width
="wrap_content" android:text="@string/get_returnvalue"
android:id
="@+id/btnReturn"></Button>
</LinearLayout>

View Code private void get_returnvalue() {
Button btnReturn
= (Button) findViewById(R.id.btnReturn);
tv
= (TextView) this.findViewById(R.id.textViewReturn);

btnReturn.setOnClickListener(
new OnClickListener() {

public void onClick(View v) {
Intent toNextInt
= new Intent();
toNextInt.setClass(ActivityUse.
this, Login.class);
startActivityForResult(toNextInt, REQUESR_ASK);
}
});
}

/*
* 通過重載這個方法,得到返回的結(jié)果 requestCode 開啟請求Intent時對應(yīng)的請求碼 resultCode 返回的結(jié)果驗證碼 data
* 返回的Intent
*
* @see android.app.Activity#onActivityResult(int, int,
* android.content.Intent)
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUESR_ASK) {
if (resultCode == RESULT_CANCELED) {
setTitle(
"cancel......");
}
else if (resultCode == RESULT_OK) {
showBundle
= data.getExtras();// 得到返回的包
name = showBundle.getString("loginName");
pass
= showBundle.getString("logPass");
tv.setText(
"您的用戶名是 " + name + " 您的密碼是 " + pass);
}
}
}

需要在ActivityUse中加入,這個是設(shè)置請求,REQUESR_ASK可以設(shè)定任何值

??? Intent toNextInt = new Intent();
????toNextInt.setClass(ActivityUse.this, Login.class);
????startActivityForResult(toNextInt, REQUESR_ASK);

@Override
?protected void onActivityResult(int requestCode, int resultCode, Intent data)

在login.java端可以取值并返回

??? Intent backIntent = new Intent();
????Bundle stringBundle = new Bundle();
????stringBundle.putString("loginName", etName.getText().toString());
????stringBundle.putString("logPass", etPass.getText().toString());
????backIntent.putExtras(stringBundle);
????setResult(RESULT_OK, backIntent);

Run一下看下結(jié)果

?

點擊“得到返回的數(shù)據(jù)”按鈕



輸入信息并點擊登陸



返回的結(jié)果為剛才輸入的結(jié)果。

?

總結(jié)

以上是生活随笔為你收集整理的android使用Activity的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。