yam*_*ams 3 android textview android-activity
我有一个Activity叫MainActivity.java的activity_main.xml文件。但是我不知道如何创建一个链接来启动另一个名为的活动DirectionActivity。以下是我的代码示例。
strings.xml
<?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>
Run Code Online (Sandbox Code Playgroud)
activity_main.xml文本视图
<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"
/>
Run Code Online (Sandbox Code Playgroud)
MainActivity.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;
}
}
Run Code Online (Sandbox Code Playgroud)
DirectionActivity.java
public class DirectionActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainMenuActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
setContentView(R.layout.activity_direction);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么,我需要怎么做才能获得“请单击此处查看路线”链接的链接,该链接将打开DirectionActivity?
您想做两件事。首先,您想要在单击时发生一些事情TextView。其次,您要启动一个新的Activity。
要完成第一件事,在活动类的onCreate方法中,您需要类似以下内容:
final TextView txtView = this.findViewById(R.id.txtView);
txtView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
Run Code Online (Sandbox Code Playgroud)
要完成第二件事,如果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));
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11640 次 |
| 最近记录: |