0

主要活动

package com.Kevious.Kevin;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
import android.opengl.*;
import android.content.*;
import android.text.*;
import java.net.*;

public class MainActivity extends Activity
{
String passw;
String value = "Test";
int i = 1;
TextView textView;
Button button;
EditText editText;


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View p1)
{
loadMethod();

}

}
);

editText = (EditText) findViewById(R.id.editText);

editText.addTextChangedListener(new TextWatcher(){

public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
}

public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
{

// TODO: Implement this method

passw = editText.getText().toString();
textView.setText(passw);
passw = textView.getText().toString();
}

public void afterTextChanged(Editable p1)
{

}
}
);
}

public void loadMethod()
{
passwCheck();
}


public void passwCheck(){

passw = textView.getText().toString();

if (passw.equals("kev"))
{
Toast.makeText(this, "corrrect " + passw, Toast.LENGTH_SHORT).show();
setContentView(R.layout.nextactivity);
} else {
Toast.makeText(this, "incorrect " + passw, Toast.LENGTH_SHORT).show();
}
}
}

下一个活动

package com.Kevious.Kevin;
import android.os.*;
import android.content.*;
import android.app.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;

public class NextActivity extends Activity
{
Button button2;
TextView textView2;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText("Welcome Kevin");
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener(){
public void onClick(View p1)
{
textView2.setText("button clicked");
buttonclick2();
}
}
);
}


public void buttonclick2(){


Toast.makeText(this, "logged out", Toast.LENGTH_SHORT).show();

setContentView(R.layout.main);
}
}

我是stackoverflow的新手,所以我希望我能正确地创建它。如果我做错了,我深表歉意。

我正在学习 Android 开发,只是在试验和创建我的第一个应用程序,但不知何故,我遇到了一个问题,即我的一个按钮不起作用,即使我正确添加了按钮侦听器也是如此。

当我在主布局上键入“kev”作为密码并按下按钮时,它将通过“setContentView(.....)”将我带到 NextActivity 布局,这很好。

问题是,当我在 NextActivity 屏幕中时,它会有一个按钮。不知何故,当我单击该按钮时,它什么也不做。我确信代码是正确的。有人可以帮我吗?

谢谢

4

5 回答 5

4

setContentView()不会开始新的活动。为此,请运行以下内容:

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);

setContentView在新的 Activity 中运行,并在那里设置任何 onClickListeners。

于 2013-05-28T16:29:37.197 回答
2

Other than the answers posted. You are trying to initialize views without setting the content to the activity in NextActivity. You can findviewbyid of the views of the current view hierarchy set to the activity.

If you need to navigate from one activity to another say on button click

     startActivity(new Intent(ActivityName.this,NextActivity.class); or
     startActiivty(new Intent("packagename.NextActivity); 

setContentView. You have misunderstood the purpose of setContentView.

http://developer.android.com/reference/android/app/Activity.html#setContentView(int)

Set the activity content to an explicit view.

This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.

Parameters

     view   The desired content to display.

In your NextActivity

      @Override 
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main); 
       // this should be first not in buttonclick2()
      textView2 = (TextView) findViewById(R.id.textView2);
       // then initialize views.  
      ...
      } 

Example:

  public class MainActivity extends Activity
 {
 String passw;
 TextView textView;
 Button button;
 EditText editText;


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View p1)
{
  passw = editText.getText().toString();
  if (passw.equals("kev"))
  {
  Toast.makeText(MainActivity.this, "corrrect " + passw, Toast.LENGTH_SHORT).show();
  startActivity(new Intent(MainActivity.this,NextActivity.class));
  } else {
  Toast.makeText(MainActivity.this, "incorrect " + passw, Toast.LENGTH_SHORT).show();
  }
}
});
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:inputType="textPassword" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="83dp"
    android:text="Password" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="39dp"
    android:text="Button" />

</RelativeLayout>

NextActivity.java

public class NextActivity extends Activity
{
Button button2;
TextView textView2;
@Override 
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.next);
  textView2 = (TextView) findViewById(R.id.textView1);
  textView2.setText("Welcome Kevin");
  button2 = (Button) findViewById(R.id.button1);
  button2.setOnClickListener(new OnClickListener(){
  public void onClick(View p1)
  {
    textView2.setText("button clicked");
  }
  });
 }
 } 

next.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="TextView" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentBottom="true"
    android:text="Button" />

</RelativeLayout>

Manifest.xml

    <activity
        android:name="com.example.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.NextActivity"
        android:label="@string/app_name" >

    </activity>
于 2013-05-28T16:43:31.347 回答
0
if (passw.equals("kev"))
{
Toast.makeText(this, "corrrect " + passw, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
} else {
Toast.makeText(this, "incorrect " + passw, Toast.LENGTH_SHORT).show();
}
}

你想为你的下一个活动使用一些东西吗

Intent intent = new Intent(getApplicationContext(), NextActivity.class);
intent.putExtra("your_key", your_value);
intent.putExtra("your_next_key", your_next_value);
startActivity(intent);

并且不要忘记在您的清单中注册您的两项活动!确保它的拼写像你的 .java 类(区分大小写!)

AndroidManifest.xlm

  <application
        android:icon="@drawable/logo"
        android:label="@string/app_name" >
       <activity
            android:name=".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=".NextActivity" />
  </application>
于 2013-05-28T16:46:15.650 回答
0

实际上,您并没有导航到 NextActivity。您在 MainActivity 本身中,您刚刚动态更改了布局。为了导航到 NextActivity 使用,

startActivity(new Intent(MainActivity.this,NextActivity.class));

在 NextActivity 的 onCreate() 方法中设置你的布局并尝试你想要做的事情。

于 2013-05-28T16:31:47.843 回答
0

setContentView()不建议在同一个 Activity 中多次调用。请参阅:Android:使用新活动切换屏幕或仅更改内容视图

此外,您需要通过以下方式启动 NextActivity:

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);

现在,您只是在更改 UI 而不添加 Activity 的功能。

于 2013-05-28T16:38:16.343 回答