Some tooltips for Android

1. When testing, how to implement a hint

can use

Toast.makeText(this, "This is a prompt", Toast.LENGTH_SHORT).show();
//Get the prompt information from the resource file string.xml
Toast.makeText(this, getString(R.string.welcome), Toast .LENGTH_SHORT).show();

The prompt will disappear after a few seconds


2. You can use AlertDialog.Builder to generate a prompt box.

   For example something like messagebox

   new AlertDialog.Builder(this)
     .setTitle("Android prompt")
     .setMessage("This is a prompt, please confirm")
     .show();

with an OK dialog

new AlertDialog.Builder(this)
          .setMessage("This is the second alert")
          .setPositiveButton("OK",
                         new DialogInterface.OnClickListener(){
                                 public void onClick(DialogInterface dialoginterface, int i){
                                     //Button event
                                  }
                          } )
          .show();


AlertDialog.Builder also has many complex usages, with dialogs for confirming and canceling

new AlertDialog.Builder(this)
         .setTitle("Prompt")
         .setMessage("Are you sure to quit?")
         .setIcon(R.drawable.quit)
         .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick( DialogInterface dialog, int whichButton) {
         setResult(RESULT_OK);//OK button event
         finish();
         }
         })
         .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
         / /Cancel button event
         }
         })
         .show();

3.Usage of menu.

public static final int ITEM_1_ID = Menu.FIRST;
public static final int ITEM_2_ID = Menu.FIRST + 1;
public static final int ITEM_3_ID = Menu.FIRST + 2;
    
public boolean onCreateOptionsMenu(Menu menu) {
         super.onCreateOptionsMenu(menu);
//不带图标的menu
         menu.add(0, ITEM_1_ID, 0, "item-1");       
//带图标的menu
         menu.add(0, ITEM_2_ID, 1, "item-2").setIcon(R.drawable.editbills2);
         menu.add(0, ITEM_3_ID, 2, "item-3").setIcon(R.drawable.billsum1);
        return true;
}

public boolean onOptionsItemSelected(MenuItem item){
       switch (item.getItemId()) {
       case 1:
            Toast.makeText(this, "menu1",Toast.LENGTH_SHORT).show();            
           return true;
       case 2:
        
           return true;
       case 3:
         
           return true;
        }
       return false;
     }

4. Activity switching

     2 Activity switching, no data transfer

//从A到B
Intent intent = new Intent();
                 intent.setClass(A.this, B.class);
                 startActivity(intent);

Pass data between 2 activities

    Several related functions
     startActivityForResult
    public final void setResult(int resultCode, String data)
    callback function

    protected void onActivityResult(int requestCode, int resultCode, Intent data)


    For example A to B, get data from B 

//A到B
static final int RG_REQUEST = 0;
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivityForResult(intent,RG_REQUEST);
                                  
//在B中处理
Bundle bundle = new Bundle();
bundle.putString("DataKey", edittext.getText().toString());//给bundle 写入数据
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();

//Finally receive data in A's callback function
if (requestCode == RG_REQUEST) {
     if (resultCode == RESULT_CANCELED)
           setTitle("Canceled...");
     else if(resultCode == RESULT_OK) {
          setTitle((String) data.getCharSequenceExtra("DataKey"));
        }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485897&siteId=291194637