2011-10-19 104 views
0

首先,我必须写出我刚刚开始使用android进行冒险,我正在构建简单的菜单,并且据我介绍并退出按钮工作:),我试着用start按钮,但它不听所有,如果您有任何建议,有什么用它做我会更乐意:)Android上点击监听器的困难

主要活动:

public class MainActivity extends Activity implements OnClickListener 
{ 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
View startButton = findViewById(R.id.start_button); 
startButton.setOnClickListener(this); 
View whereButton = findViewById(R.id.where_button); 
whereButton.setOnClickListener(this); 
View aboutButton = findViewById(R.id.about_button); 
aboutButton.setOnClickListener(this); 
View exitButton = findViewById(R.id.exit_button); 
exitButton.setOnClickListener(this); 
} 

public void onClick(View v) { 

switch (v.getId()) { 

case R.id.about_button: 
Intent i = new Intent(this, About.class); 
startActivity(i); 
break; 
case R.id.exit_button: 
finish(); 
break; 
case R.id.start_button: 
i = new Intent(this, Start.class); 
startActivity(i); 
break; 
} 
} 

的strings.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="app_name">City Inspector</string> 
<string name="main_title">Ldz Inspector</string> 
<string name="start_title">Profile</string> 
<string name="start_label">START</string> 
<string name="where_label">WHERE AM I?</string> 
<string name="option_label">OPTIONS</string> 
<string name="about_label">ABOUT</string> 
<string name="create_label">CREATE PROFILE</string> 
<string name="browse_label">BROWSE MAP</string> 
<string name="exit_label">EXIT</string> 
<string name="about_text">\ 
about program things 

</string> 
</resources> 

start.java

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.start); 
} 
} 

start.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@color/background" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:padding="20dip" 
android:orientation="horizontal" > 
<LinearLayout 
android:orientation="vertical" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:layout_gravity="center" > 
<TextView 
android:text="@string/start_title" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_gravity="center" 
android:layout_marginBottom="25dip" 
android:textSize="24.5sp" /> 
<Button 
android:id="@+id/Create_Profile" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/create_label" /> 
<Button 
android:id="@+id/Browse_Map" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/browse_label" /> 
</LinearLayout> 
</LinearLayout> 

!编辑:

对不起,我知道我忘了张贴的东西:),这里是AndroidManifest :)

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="menu.dot" 
android:versionCode="1" 
android:versionName="1.0"> 
<application android:label="@string/app_name" android:icon="@drawable/icon"> 
<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=".About"> 
android:label="@string/about_title" 
android:theme="@android:style/Theme.Dialog" > 
</activity> 
<activity android:name=".Exit"> 
andorid:label="@string/exit_title">  
</activity> 
<activity android:name=".Start"> 
andorid:label="@string/start_title"> 

</activity> 
/application> 
</manifest> 

请检查start.xml版

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@color/background" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:padding="20dip" 
android:orientation="horizontal" > 
<LinearLayout 
android:orientation="vertical" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:layout_gravity="center" > 
<TextView 
android:text="@string/main_title" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_gravity="center" 
android:layout_marginBottom="25dip" 
android:textSize="24.5sp" /> 
<Button 
android:id="@+id/start_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/start_label" /> 
<Button 
android:id="@+id/where_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/where_label" /> 
<Button 
android:id="@+id/option_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/option_label" /> 
<Button 
android:id="@+id/about_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/about_label" /> 
<Button 
android:id="@+id/exit_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/exit_label" /> 
</LinearLayout> 
</LinearLayout> 
+0

是否DDMS告诉你什么?给你关于为什么它不开始你的活动的任何信息? – JQCorreia

+0

据我所见,一切看起来都很好。我看不到:start.class是从Activity派生出来的吗? start.class是否在清单中声明?同时在Log.d(“mainActivity”,“onClick triggered”)中放入一个日志在onClick()方法内部,并在每个case语句内部查看它是否被触发 –

+0

thx rafael T,我会在一秒钟内检查它 – iie

回答

0

在Android中,你并不需要实现onClickListener和检查哪个按钮被点击。 Android为您提供了一种简洁的方式来指定用于处理onClick事件的方法,方法是将onClick-attribute用于XML-Layout文件中的按钮。

考虑以下布局:

<Button android:id="@+id/some_button" 
    android:onClick="someButtonsAction" 
    android:layout_width="fill_parent" 
    android:layout_height="match_content" 
/> 

的活动呈现该按钮现在需要有这样的方法:

public void someButtonsAction(View v){ 
    // Do your onClick stuff here 
} 

供给的View v -argument是点击视图本身。整个事情也说明in the docs如果页面顶部。

这使您有机会使代码更具可读性,并且不需要从布局中获取所有按钮,将它们添加到onClickListener中并确定哪个按钮被单击(在侦听器中)。

+0

谢谢大家的耐心,你今天帮了我很多:) – iie

+0

@ user552629所以,你应该投票选好答案并接受最好的答案。 –

+0

我不能投票。声望是低 – iie

0

您是否在AndroidManifest.xml中注册了'开始'活动?
如果您没有在AndroidManifest.xml中注册活动,则无法运行活动。

+0

请检查第一篇文章的最新版本。 – iie

0

你可以发布你的main.xml吗?我尝试了你的代码,假设你的main.xml看起来像什么,并开始活动工作得很好(即,开始布局显示)。

+0

我已经把它放在第二个以前的主帖 – iie

+0

你的代码适合我。我不得不首先清理字符串资源xml的底部,因为你有2个exit_label字符串,而你的“about_text”字符串给了我空指针异常: EXIT \ 关于程序事物 EXIT

+0

嗯,这太奇怪了。我不知道你是否检查过更新后的版本[做了一个小时或更久以前],但现在问题看起来像这样:当我按开始时,它会带我到关于页面,当我点击它时带我到页。 – iie

0

mainActivity.java

public class MainActivity extends Activity implements OnClickListener 
{ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
View startButton = findViewById(R.id.start_button); 
startButton.setOnClickListener(this); 
View whereButton = findViewById(R.id.where_button); 
whereButton.setOnClickListener(this); 
View aboutButton = findViewById(R.id.about_button); 
aboutButton.setOnClickListener(this); 
View exitButton = findViewById(R.id.exit_button); 
exitButton.setOnClickListener(this); 
} 

public void onClick(View v) { 


switch (v.getId()) { 

case R.id.about_button: 
Intent i = new Intent(this, About.class); 
startActivity(i); 
break; 
case R.id.exit_button: 
finish(); 
break; 

} 
} 
public void StartCl(View v){ 
Intent idd = new Intent(this,Start.class); 
startActivity(idd); 
} 
} 

main.xml中

<Button 
android:id="@+id/start_button" 
android:onClick="StartCl" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/start_label" /> 
+0

您正在布局和代码中分配'onClick'-Listener。删除代码内分配,它应该工作。此外,您应该为您的问题添加详细信息,而不是创建新的答案,除非您想回​​答自己的帖子。 –

+0

嗯你的意思是踢出这部分公共无效onClick(查看v){}?但是当我删除它时,我用startButton.setOnClickListener(this)得到了一些错误; – iie

+0

确定它现在工作不知何故,将看到多久:) – iie