我有一个名为Activity
的{{1}},其中包含MainActivity.java
个文件。但我不知道如何创建一个链接来启动另一个名为activity_main.xml
的活动。以下是我的代码示例。
的strings.xml
DirectionActivity
activity_main.xml textview
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">EEMA</string>
<string name="action_settings">Settings</string>
<string name="hello_world">The Emergency Evacuation Mobile App!</string>
<string name="directions">\n\nTo view directions click here!</string>
<string name="title_activity_direction">DirectionActivity</string>
<string name="title_activity_main">MainActivity</string>
</resources>
MainActivity.java
<TextView android:id="@+id/txtView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp"
android:textColorLink="#FFFF00"
android:textStyle="bold"
/>
DirectionActivity.java
public class MainMenuActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
String str = "Please click here to view Directions";
TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setText(str);
Linkify.addLinks(txtView, Linkify.ALL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
那么我需要做些什么才能获得请点击此处查看方向以显示为打开DirectionActivity的链接?
答案 0 :(得分:4)
你想要做两件事。首先,您希望点击TextView
后发生一些事情。其次,您要发布新的Activity
。
要完成第一件事,在您的活动类“onCreate
方法中,您需要以下内容:
final TextView txtView = this.findViewById(R.id.txtView);
txtView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
要完成第二件事,如果您要发起的Activity
被称为DirectionActivity
:
final TextView txtView = this.findViewById(R.id.txtView);
txtView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainMenuActivity.this.startActivity(new Intent(MainMenuActivity.this, DirectionActivity.class));
}
});