android-intent隐式跳转
android-intent隐式跳转
- 首先两个界面第一个界面有两个EditText用于输入账号密码,还有两个Button用于登陆和取消
第二个界面是两个TextView用于显示第一个界面传过来的账号密码。
- 在Manifest.xml中显示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstintent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
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=".SecondActivity"
android:label="@string/title_activity_second" >
<intent-filter>
<action android:name="android.intent.action.Second"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest> - MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.Menu;
public class MainActivity extends Activity {
Button bt1;
Button bt2;
EditText et1;
EditText et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button1);
bt2 = (Button)findViewById(R.id.button2);
et1 = (EditText)findViewById(R.id.editText1);
et2 = (EditText)findViewById(R.id.editText2);
final String username = et1.getText().toString();
final String password = et2.getText().toString();
bt1.setOnClickListener(new OnClickListener() {
@Override//设置侦听
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.Second");
if((username.equals("admin"))&&(password.equals("123"))){
intent.putExtra("name", username);
intent.putExtra("pwd", password);
startActivity(intent);
}else{//弹出密码错误
Toast.makeText(getApplicationContext(), "账号密码输入错误", Toast.LENGTH_SHORT).show();
}
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplication(), "登陆已取消", Toast.LENGTH_SHORT ).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
} - SecondActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class SecondActivity extends Activity {
TextView tv1;
TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv1 = (TextView)findViewById(R.id.view1);
tv2 = (TextView)findViewById(R.id.view2);
Intent intent = this.getIntent();
tv1.setText(intent.getStringExtra("name"));
tv2.setText(intent.getStringExtra("pwd"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!