我还是初学者在android中。 如何让android onclicklistener进行下一个活动,我的意思是,我创建了RegisterActivity,当用户注册时,程序应该是MainActivity但它转到SingleMenuItemActivity。谁能告诉我我想做什么?非常感谢你。 抱歉我的英语不好。
package unai.skripsi.bandungtourgiude;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SingleMenuItemActivity extends Activity {
// JSON node keys
private static final String in_nama_tempat = "nama_tempat";
private static final String in_alamat_tempat = "alamat_tempat";
private static final String in_no_tempat = "no_tempat";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String nama = in.getStringExtra(in_nama_tempat);
String alamat = in.getStringExtra(in_alamat_tempat);
String nomor = in.getStringExtra(in_no_tempat);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.nama_tempat);
TextView lblCost = (TextView) findViewById(R.id.alamat_tempat);
TextView lblDesc = (TextView) findViewById(R.id.no_tempat);
lblName.setText(nama);
lblCost.setText(alamat);
lblDesc.setText(nomor);
Button map = (Button) findViewById(R.id.button1);
map.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(SingleMenuItemActivity.this, Pariwisata.class));
}
});
Button call = (Button) findViewById(R.id.button2);
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(SingleMenuItemActivity.this, Telfon.class));
}
});
Button text = (Button) findViewById(R.id.button3);
text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(SingleMenuItemActivity.this, RegisterActivity.class));
}
});
}
}
RegisterActivity.java
package unai.skripsi.bandungtourgiude;
import static unai.skripsi.bandungtourgiude.CommonUtilities.SENDER_ID;
import static unai.skripsi.bandungtourgiude.CommonUtilities.SERVER_URL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class RegisterActivity extends Activity {
// alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Internet detector
ConnectionDetector cd;
// UI elements
EditText txtName;
EditText txtEmail;
// Register button
Button btnRegister;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(RegisterActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Check if GCM configuration is set
if (SERVER_URL == null || SENDER_ID == null || SERVER_URL.length() == 0
|| SENDER_ID.length() == 0) {
// GCM sernder id / server url is missing
alert.showAlertDialog(RegisterActivity.this, "Configuration Error!",
"Please set your Server URL and GCM Sender ID", false);
// stop executing code by return
return;
}
txtName = (EditText) findViewById(R.id.txtName);
txtEmail = (EditText) findViewById(R.id.txtEmail);
btnRegister = (Button) findViewById(R.id.btnRegister);
/*
* Click event on Register button
* */
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Read EditText dat
String name = txtName.getText().toString();
String email = txtEmail.getText().toString();
// Check if user filled the form
if(name.trim().length() > 0 && email.trim().length() > 0){
// Launch Main Activity
Intent i = new Intent(getApplicationContext(), MainActivity.class);
// Registering user on our server
// Sending registraiton details to MainActivity
i.putExtra("name", name);
i.putExtra("email", email);
startActivity(i);
finish();
}else{
// user doen't filled that data
// ask him to fill the form
alert.showAlertDialog(RegisterActivity.this, "Registration Error!", "Please enter your details", false);
}
}
});
}
}
我的AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="unai.skripsi.bandungtourgiude"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="unai.skripsi.bandungtourguide.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="unai.skripsi.bandungtourguide.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="unai.skripsi.bandungtourgiude.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="unai.skripsi.bandungtourgiude.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name="unai.skripsi.bandungtourgiude.DashboardActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RegisterActivity"
android:label="@string/app_name"></activity>
<!-- Main Activity -->
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="unai.skripsi.bandungtourgiude" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
<!-- Login Activity -->
<activity
android:label="Silahkan login"
android:name=".Fungsilogin"></activity>
<!-- Register Activity -->
<activity
android:label="Silahkan mendaftar"
android:name=".Pendaftaran"></activity>
<activity
android:label="tes"
android:name=".TesAsyncTaskActivity"></activity>
<activity
android:label="PILIHAN TEMPAT"
android:name=".SingleMenuItemActivity"></activity>
<activity
android:label="rumah sakit"
android:name=".RumahSakit"></activity>
<activity
android:label="hotel"
android:name=".Hotel"></activity>
<activity
android:label="ATM"
android:name=".Atm"></activity>
<activity
android:label="PARIWISATA"
android:name=".Pariwisata"></activity>
<activity
android:label="KANTOR POLISI"
android:name=".Polisi"></activity>
<activity
android:label="KULINER"
android:name=".Kuliner"></activity>
<activity
android:label="OUTLET"
android:name=".Outlet"></activity>
<activity
android:label="OUTLET"
android:name=".Map"></activity>
<activity
android:label="rumah sakit"
android:name=".Semuatempat"></activity>
<activity
android:label="Telfon operator"
android:name=".Telfon"></activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAkJYisD9-6HB8D1asdeTTTZVtgNCLg8Fyk" />
</application>
</manifest>
答案 0 :(得分:0)
Intent过滤器确定要启动的内容。
按以下方式更改您的清单
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
并删除
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
来自Dashboard活动的。 HTH。
答案 1 :(得分:0)
在btnRegister onClick侦听器中,删除 startActivity 和完成。
Intent i = new Intent(getApplicationContext(), MainActivity.class);
// Registering user on our server
// Sending registraiton details to MainActivity
i.putExtra("name", name);
i.putExtra("email", email);
RegisterActivity.this.startActivity(i);
如果您不想在历史堆栈中使用RegisterActivity,请在清单文件中为活动标记设置属性android:noHistory=true
,而不是finish()方法。