zoukankan      html  css  js  c++  java
  • Android:Intent

    Intent intent = getIntent();

    Intent intent2 = new Intent();

    通常通过getIntent()方法获取到是非空的,即类似于:

    intent:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.menutest/.MainActivity }

    intent2 是通过new 方式实例化的,肯定为null。

    一般情况下我们需要用getIntent()方式来获取到本Activity的intent。

    intent通常用于跳转,inent有个setAction()方法,官方文档的解释如下:

    Intent android.content.Intent.setAction(String action)

    Set the general action to be performed.

    Parameters:
    action An action name, such as ACTION_VIEW. Application-specific actions should be prefixed with the vendor's package name.
    Returns:
    Returns the same Intent object, for chaining multiple calls into a single statement.
    See Also:
    getAction
    实际上,设置action即,setAction(String action)这个方法通常是用来判断是点击了哪个按钮,然后来做相应的判断以及数据处理。
    有如下场景,比如之前的那个记事本的应用实例里,
    如何判断是点了添加新日记还是检测呢,那么就可以在public boolean onOptionsItemSelected(MenuItem item) {}这个方法里进行判断,如果点了添加新日记,那么我们就给新建一个intent0,给这个intent0绑定一个action即绑定一个字符串,这个所谓的action实际上就是一个字符串。具体代码如下:
     1 public boolean onOptionsItemSelected(MenuItem item) {
     2 
     3         switch (item.getItemId()) {
     4         case MENU_ITEM_INSERT:
     5 
     6             Intent intent0 = new Intent(this, MyDiaryEdit.class);
     7             //设置action,跳到第二个页面的时候就通过这个action来判断点了哪个按钮
     8             intent0.setAction(MyDiaryEdit.INSERT_TEXT_ACTION);
     9             System.out.println("intent0已经设置了Action,");
    10             /*
    11              * getIntent():Return the intent that started this activity 
    12              * getIntent().getData(): Retrieve data this intent is operating on.
    13              * Returns,The URI of the data this intent is targeting or null.
    14              */
    15             intent0.setData(getIntent().getData());//intent设置URI
    16             System.out.println("Return the intent that started this activity,the intent:"+getIntent());
    17             startActivity(intent0);
    18             return true;
    19 
    20         case MENU_ITEM_EDIT:
    21 
    22             Intent intent1 = new Intent(this, MyDiaryEdit.class);
    23             intent1.setAction(MyDiaryEdit.EDIT_TEXT_ACTION);
    24             intent1.setData(item.getIntent().getData());
    25             startActivity(intent1);
    26             return true;
    27 
    28         case MENU_ITEM_DELETE:
    29             Uri uri = ContentUris.withAppendedId(getIntent().getData(),
    30                     getListView().getSelectedItemId());
    31             getContentResolver().delete(uri, null, null);
    32             rewindView();
    33         }
    34 
    35         return super.onOptionsItemSelected(item);
    36     }

    第二个页面判断是点了哪个按钮的代码:

     1 protected void onCreate(Bundle savedInstanceState) {
     2         super.onCreate(savedInstanceState);
     3         setTheme(android.R.style.Theme_Black);
     4         //Return the intent that started this activity. 
     5         final Intent intent = getIntent();
     6         //获取intent里到底是绑定action字符串,下面用来判断是点了哪个按钮
     7         final String action = intent.getAction();
     8         setContentView(R.layout.mydiaryedit);//添加对应的layout
     9         System.out.println("intent.getAction():"+intent.getAction());
    10         myToast("intent.getAction():"+intent.getAction());
    11         TextView wyl_tv = (TextView) findViewById(R.id.wyl_txt);
    12         wyl_tv.setText("intent.getAction():"+intent.getAction()+";
    
    "
    13                 + " URI uri = intent.getData(): "+intent.getData()+";
    
     MyDiaryEdit.java的intent:"+getIntent());
    14         mTitleText = (EditText) findViewById(R.id.title);
    15         mBodyText = (EditText) findViewById(R.id.body);
    16 
    17         confirmButton = (Button) findViewById(R.id.confirm);
    18         modifyButton = (Button) findViewById(R.id.modified);
    19 
    20         if (EDIT_TEXT_ACTION.equals(action)) {
    21             /*
    22              * EDIT_TEXT_ACTION.equals(action),EDIT_TEXT_ACTION就是前一个
    23              * 页面中intent.setAction的时候设置的哪个字符串,这样就能够判断是不是点了新增日记的按钮了
    24              */
    25             mState = STATE_EDIT;
    26             mTitleText.setEnabled(false);//设置为不可编辑
    27             mBodyText.setEnabled(false);//设置不可编辑
    28             modifyButton.setVisibility(View.VISIBLE);
    29             mUri = intent.getData();
    30             mCursor = managedQuery(mUri, projection, null, null, null);
    31             mCursor.moveToFirst();
    32             String title = mCursor.getString(1);
    33             mTitleText.setTextKeepState(title);
    34             String body = mCursor.getString(2);
    35             mBodyText.setTextKeepState(body);
    36 
    37             setResult(RESULT_OK, new Intent(MyDiaryEdit.EDIT_TEXT_ACTION, mUri));
    38             setTitle("编辑日记");
    39         } else if (INSERT_TEXT_ACTION.equals(action)) {
    40             mState = STATE_INSERT;
    41             setTitle("新建日记");
    42         } else {
    43 
    44             Log.e(TAG, "no such action error");
    45             MyDiaryEdit.this.finish();
    46 
    47             return;
    48         }
    49 
    50         confirmButton.setOnClickListener(new MyBtnOnClickListen());
    51         modifyButton.setOnClickListener(new MyBtnOnClickListen());
    52 
    53     }
  • 相关阅读:
    六、TreeSet中的定制排序
    五、Set接口
    四、List接口
    Vocabulary Recitation 2020/05/20
    Vocabulary Recitation 2020/05/19
    Vocabulary Recitation 2020/05/18
    自控力_第六章
    Vocabulary Recitation 2020/05/17
    Vocabulary Recitation 2020/05/15
    Vocabulary Recitation 2020/05/14
  • 原文地址:https://www.cnblogs.com/Sunnor/p/4799214.html
Copyright © 2011-2022 走看看