MainActivity
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();
}
}
}
NextActivity
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 Layout,这样可以正常工作。
问题是,当我在NextActivity屏幕时,它会有一个按钮。不知怎的,当我点击那个按钮时,它什么也没做。我确信代码是对的。有人可以帮助我吗?
由于
答案 0 :(得分:4)
setContentView()
无法启动新活动。要做到这一点,运行这样的东西:
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
在新的Activity中运行setContentView
,然后在那里设置任何onClickListeners。
答案 1 :(得分:2)
除了发布的答案。您正在尝试初始化视图而不将内容设置为NextActivity中的活动。您可以通过查看当前视图层次结构的视图来查看活动。
如果您需要从一个活动导航到另一个活动,请点击按钮
startActivity(new Intent(ActivityName.this,NextActivity.class); or
startActiivty(new Intent("packagename.NextActivity);
的setContentView。您误解了setContentView的用途。
http://developer.android.com/reference/android/app/Activity.html#setContentView(int)
将活动内容设置为显式视图。
此视图直接放置在活动的视图层次结构中。它本身可以是一个复杂的视图层次结构。调用此方法时,将忽略指定视图的布局参数。默认情况下,视图的宽度和高度都设置为MATCH_PARENT。要使用您自己的布局参数,请调用setContentView(android.view.View,android.view.ViewGroup.LayoutParams)。
参数
view The desired content to display.
在你的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.
...
}
示例:
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>
答案 2 :(得分:0)
实际上你并没有导航到NextActivity。您处于MainActivity本身,您刚刚动态更改了布局。要导航到NextActivity,请使用
startActivity(new Intent(MainActivity.this,NextActivity.class));
在NextActivity的onCreate()方法中设置你的布局并尝试你想要做的事情。
答案 3 :(得分:0)
不建议在同一个活动中多次调用setContentView()
。请参阅:Android: switching screens with new activity or just changing content view。
此外,您需要通过以下方式启动NextActivity:
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
目前,您只是在不添加活动功能的情况下更改UI。
答案 4 :(得分: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>