我想在Android应用中创建电子邮件链接。 如果我这样做:
<TextView
android:text="xxxx@yyyy.zz"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:autoLink="email"/>
然后它可以正常工作。
但是,我确实希望在代码中执行此操作。以下示例是Android Studio中具有基本活动的新Android项目。我添加了标记的代码并将id“mytest”添加到自动生成的布局中。package com.example.bjursus.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.util.Linkify;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Added code - start
getIntent().setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ConstraintLayout clo = (ConstraintLayout) findViewById(R.id.mytest);
TextView tv = new TextView(getApplicationContext());
tv.setText("zxxx@yyyy.zz");
Linkify.addLinks(tv, Linkify.EMAIL_ADDRESSES);
tv.setLinksClickable(true);
clo.addView(tv);
// Added code - end
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这会给我一个电子邮件链接,但是当我点击它时,我得到了这个例外:
AndroidRuntimeException:从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?
有什么建议吗?
答案 0 :(得分:0)
只需将其添加到代码
即可getIntent().setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
这将告诉您的活动可以开始新任务。