1.四大元件之activity重新學習(六小時視訊)
阿新 • • 發佈:2021-09-22
intent就是元件和元件之間的跳轉,比如說後期的服務等等,都要用到intent
跳轉並傳值(靜態註冊)
package com.example.myactivity77; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.widget.Button; import android.widget.EditText;import android.widget.TextView; public class MainActivity extends AppCompatActivity { EditText userName; EditText userPassword ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); userName= findViewById(R.id.userName); userPassword = findViewById(R.id.userPassword); } public void denglu(View view) { String userNameText = userName.getText().toString(); String userPasswordText = userPassword.getText().toString(); //設定意圖 Intent intent = newIntent(this,LoginSuccess.class); //傳值 intent.putExtra("userName",userNameText); intent.putExtra("userPassword",userPasswordText); //跳轉 startActivity(intent); } }
接收值
package com.example.myactivity77; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class LoginSuccess extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_success); TextView textView = findViewById(R.id.text11); Intent intent = getIntent(); String userName = intent.getStringExtra("userName"); String userPassword = intent.getStringExtra("userPassword"); textView.setText("使用者名稱+密碼:"+userName+" "+userPassword); } }
/****************************************************************************************************************************************************************************************/
Intent和Intent過濾器(元件之間的隱式跳轉)
不指定特定的類名,而是宣告要執行的常規操作,從而允許其他應用中的元件來處理它
在配置檔案中配置意圖過濾
<activity android:name=".ThirdActivity" android:exported="true" > <intent-filter> <action android:name="baga_shit"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
public void tiaozhuan(View view) { Intent intent = new Intent(); intent.setAction("baga_shit"); intent.addCategory(Intent.CATEGORY_DEFAULT); //傳值 String userNameText = userName.getText().toString(); String userPasswordText = userPassword.getText().toString(); intent.putExtra("userName",userNameText); intent.putExtra("userPassword",userPasswordText); startActivity(intent); }
/****************************************************************************************************************************************************************************************/
通過顯式意圖跳轉到第三方應用
copy /**
* 這個方法會在點選按鈕的時候執行
* @param view
*/
public void skip2Browser(View view){
Log.d(TAG,"skp2Browser");
Intent intent =new Intent();
/**
* 元件的名稱,也就是ComponentName =包名/類的路徑名稱,如包名一直,類的路徑名稱可以省略包名
* 如:com.google.android.as/com.google.android.apps.miphone.aiai.app.AiAiPredictionService
*/
//第一種寫法
// intent.setClassName("com.google.android.as","com.google.android.apps.miphone.aiai.app.AiAiPredictionService");
//第二種寫法
ComponentName componentName=new ComponentName("com.google.android.as","com.google.android.apps.miphone.aiai.app.AiAiPredictionService");
intent.setComponent(componentName);
startActivity(intent);
}
通過隱式意圖跳轉到第三方應用
copy /** * 下面通過隱式意圖來跳轉到第三方應用 * 1、建立Intent物件 * 2、第二步給intent物件,設定Action、category,5.1以上要設定包名 * 3、startActivity(intent) * @param view */ public void skip2BrowserInvisible(View view) { Intent intent =new Intent(); intent.setAction("android.intent.action.SEARCH"); intent.addCategory("android.intent.category.DEFAULT"); intent.setPackage("com.google.android.as"); startActivity(intent); }