2017-04-21 44 views
0

當我使用i.setType("text/plain");,那麼我的按鈕正在工作,但不使用時,它不工作。點擊它後沒有給出選項來打開任何應用程序。爲什麼這樣?。請幫幫我。爲什麼不使用setType()時按鈕不工作?

這裏是MainActivity代碼:

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button bt = (Button) findViewById(R.id.bt); 
     bt.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(); 
       i.setAction(Intent.ACTION_SEND); 
       i.setType("text/plain"); 
       EditText et = (EditText) findViewById(R.id.et); 
       String text = et.getText().toString(); 
       i.putExtra(Intent.EXTRA_SUBJECT,"Email From Ankit's Own 
       Created App"); 
       i.putExtra(Intent.EXTRA_TEXT,text); 

       if (i.resolveActivity(getPackageManager()) != null) { 
        startActivity(i); 
       } 
      } 
     }); 
    } 
} 
+0

哪裏有'createChooser'? –

+0

爲什麼問爲什麼?如果你的代碼工作? – faruk

回答

0

你應該在下列方式使用startActivity:

      try { 
           startActivity(Intent.createChooser(emailIntent, 
             "Send email using...")); 
          } catch (android.content.ActivityNotFoundException ex) { 
           Toast.makeText(getActivity(), 
             "No email clients installed.", 
             Toast.LENGTH_SHORT).show(); 
          } 
0

它需要一個電子郵件應用程序,讓您的數據,

一個更詳細的例子來獲取共享文件,你可以看看它here

0

試試這個,

/* Create the Intent */ 

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

/* Fill it with Data */ 
emailIntent.setType("plain/text"); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text"); 

/* Send it off to the Activity-Chooser */ 
context.startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
+0

但我的查詢是,爲什麼按鈕不適用於setType()方法。 –

相關問題