我在活动中有2个按钮。一个是注销按钮,工作正常,另一个是相机按钮,点击时应该导致另一个活动。但是,当我单击相机按钮转到下一个活动(相机功能)时,当前活动只会刷新自己,每次点击它都是额外的时间我必须单击注销按钮才能注销!(即如果我点击相机按钮5次我必须单击退出按钮5次才能注销。这可能是非常愚蠢的事情,但我是编程的新手,所以请耐心等待!以下是相关的代码段。如果有人可以提供帮助,我会非常感激。
欢迎课程
package com.example.myapp;
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;
import com.parse.ParseUser;
public class Welcome extends Activity {
// Declare Variable
Button logout;
Button cameraButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from singleitemview.xml
setContentView(R.layout.welcome);
// Retrieve current user from Parse.com
ParseUser currentUser = ParseUser.getCurrentUser();
// Convert currentUser into String
String struser = currentUser.getUsername().toString();
// Locate TextView in welcome.xml
TextView txtuser = (TextView) findViewById(R.id.txtuser);
// Set the currentUser String into TextView
txtuser.setText("You are logged in as " + struser);
// Locate Button in welcome.xml
logout = (Button) findViewById(R.id.logout);
// Logout Button Click Listener
logout.setOnClickListener(new OnClickListener() {
/** Called when the user clicks the Logout button */
public void onClick(View arg0) {
// Logout current user
ParseUser.logOut();
finish();
}
});
// Locate Button in welcome.xml
cameraButton = (Button) findViewById(R.id.cameraButton);
// Camera Button Click Listener
cameraButton.setOnClickListener(new OnClickListener() {
/** Called when the user clicks the Camera button */
public void onClick(View view) {
// Send user to Camera.class
Intent intent = new Intent(Welcome.this, Camera.class);
startActivity(intent);
}
});
}
}
欢迎xml布局文件中的按钮代码
<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/logout"
android:layout_centerHorizontal="true"
android:onClick="sendMessage"
android:text="@string/CameraBtn" />
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="ParseApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:allowBackup="true"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.myapp.LoginSignupActivity" >
</activity>
<activity android:name="com.example.myapp.Welcome" >
</activity>
<activity
android:name="com.example.myapp.Camera">
</activity>
</application>
</manifest>
字符串xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">myapp</string>
<string name="app_name">myapp</string>
<string name="Username">Username</string>
<string name="Password">Password</string>
<string name="LoginBtn">Login</string>
<string name="SignupBtn">Sign Up</string>
<string name="LogoutBtn">Log Out</string>
<string name="CameraBtn">Camera</string>
<string name="Welcome">Welcome!</string>
<string name="tap">Tap the image to open the camera!!</string>
<string name="title_camera">My Message</string>
</resources>
LoginSignup类
public class LoginSignupActivity extends Activity {
// Declare Variables
Button loginbutton;
Button signup;
String usernametxt;
String passwordtxt;
EditText password;
EditText username;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from main.xml
setContentView(R.layout.loginsignup);
// Locate EditTexts in main.xml
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
// Locate Buttons in main.xml
loginbutton = (Button) findViewById(R.id.login);
signup = (Button) findViewById(R.id.signup);
// Login Button Click Listener
loginbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Retrieve the text entered from the EditText
usernametxt = username.getText().toString();
passwordtxt = password.getText().toString();
// Send data to Parse.com for verification
ParseUser.logInInBackground(usernametxt, passwordtxt,
new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// If user exist and authenticated, send user to Welcome.class
Intent intent = new Intent(
LoginSignupActivity.this,
Welcome.class);
startActivity(intent);
Toast.makeText(getApplicationContext(),
"Successfully Logged in",
Toast.LENGTH_LONG).show();
finish();
} else {
Toast.makeText(
getApplicationContext(),
"No such user exist, please signup",
Toast.LENGTH_LONG).show();
}
}
});
}
});
// Sign up Button Click Listener
signup.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Retrieve the text entered from the EditText
usernametxt = username.getText().toString();
passwordtxt = password.getText().toString();
// Force user to fill up the form
if (usernametxt.equals("") && passwordtxt.equals("")) {
Toast.makeText(getApplicationContext(),
"Please complete the sign up form",
Toast.LENGTH_LONG).show();
} else {
// Save new user data into Parse.com Data Storage
ParseUser user = new ParseUser();
user.setUsername(usernametxt);
user.setPassword(passwordtxt);
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Show a simple Toast message upon successful registration
Toast.makeText(getApplicationContext(),
"Successfully Signed up, please log in.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Sign up Error", Toast.LENGTH_LONG)
.show();
}
}
});
}
}
});
}
}
ParseUser.logout
// Locate Button in welcome.xml
logout = (Button) findViewById(R.id.logout);
// Logout Button Click Listener
logout.setOnClickListener(new OnClickListener() {
/** Called when the user clicks the Logout button */
public void onClick(View arg0) {
// Logout current user
ParseUser.logOut();
finish();
}
});
答案 0 :(得分:0)
按钮强>
<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/logout"
android:layout_centerHorizontal="true"
android:onClick="sendMessage"
android:text="@string/CameraBtn" />
android:onClick="sendMessage"
行表示单击按钮时,应调用sendMessage
方法。除非您使用sendMessage
方法,否则我会从您的代码中删除此行。
<强>意图强>
/** Called when the user clicks the Camera button */
public void onClick(View view) {
// Send user to Camera.class
Intent intent = new Intent(Welcome.this, Camera.class);
startActivity(intent);
}
new Intent(Welcome.this, Camera.class)
是开始新活动的可接受方式。通过使用Welcome.this
代替this
,您可以正确地抓住上下文。
答案 1 :(得分:0)
解决了它,我扩展了MainActivity,而不是在我的相机类中扩展活动!!
答案 2 :(得分:-1)
我必须将此作为答案,因为我需要更多的声誉点来评论。 对于一个按钮,您基本上有两个onClick事件,因此您可能想要解决此问题。
android:onClick="sendMessage"
// Camera Button Click Listener
cameraButton.setOnClickListener(new View.OnClickListener()
有时候使用getApplicationContext()
会更好Intent intent = new Intent(getApplicationContext(), Camera.class);
startActivity(intent)
尝试将onClick方法修改为以下
// Add a click listener to perform action when button is clicked
cameraButton.setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick( View view )
{
}
});