想实现点击游戏说明后出现一个新的activity显示游戏的说明文字。如图首先先将工程的主要文件全部贴出:
1.res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="@color/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="25sp"
android:textColor="#7ba6df"
android:text="@string/activity_main_title"/>
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_lable"/>
<Button
android:id="@+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_lable"/>
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_lable"/>
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_lable"/>
</LinearLayout>
</LinearLayout>
2.res/values/string.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="activity_main_title">android sudoku</string>
<string name="continue_lable">继续游戏</string>
<string name="about_lable">游戏说明</string>
<string name="new_game_lable">新游戏</string>
<string name="exit_lable">退出游戏</string></resources>
3.src/com.example.helloandroid/MainActivity.java
package com.example.helloandroid;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}
然后开始添加点击游戏说明按钮出现新的activity显示游戏说明文字功能,书上是这样说的,不明白,求大神解释。书中步骤如下:
1.新建一个布局视图,用来显示游戏说明文字:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView
android:id="@+id/gametext_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gametext_text"
</ScrollView>
2.修改res/values/string.xml文件:
<string name="about_title">about Android Sudoku</string>
<string name="about_text">\
Sudoku is a logic-based,askldjflkasdjflaskjfiowerklfnbdkjdflkgjioerdfkllxxxxxxxxxxxx
</string>
3.新建about.java文件用来填充新的窗口,这步可以看懂。但居然我不知道怎样新建这样的一个文件,自己都不好意思了。大神指教。
package com.example.helloandroid;import android.app.Activity;
import android.os.Bundle;protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
4.补充src/com.example.helloandroid/MainActivity.java文件,用来把游戏说明按钮和用来显示游戏说明的activity连接起来,起到点击游戏说明就触发一个事件调用显示游戏说明的activity。这一步中好多不懂的地方,真心恳求大神们解释下。一个是在向四个按钮添加侦听的代码中几个this到底是this的什么东西(view aboutButton = this.findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);)。还有实现接口的文件也让我云里雾里,这个接口文件实现的什么功能。可以讲解下这个文件吗?package com.example.helloandroid;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set up click listeners for all the buttons
view continueButton = this.findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
view newButton = this.findViewById(R.id.new_button);
newButton.setOnClickListener(this);
view aboutButton = this.findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
view exitButton = this.findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public class MainActivity extends Activity implements OnClickListener{
public void onClick(View v){
switch (v.getId()){
case R.id.about_button;
Intent i = new Intent(this,About.class);
startActivity(i);
break;
//More buttons go here(if any)...
}
}
}
5.最后一步是在androidManifest.xml文件中注册新的activity。就不贴代码了。
真心恳求大神们详细讲解下。分不多了,只有三十分,真的谢谢大神们了!!!
1.res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="@color/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="25sp"
android:textColor="#7ba6df"
android:text="@string/activity_main_title"/>
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_lable"/>
<Button
android:id="@+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_lable"/>
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_lable"/>
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_lable"/>
</LinearLayout>
</LinearLayout>
2.res/values/string.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="activity_main_title">android sudoku</string>
<string name="continue_lable">继续游戏</string>
<string name="about_lable">游戏说明</string>
<string name="new_game_lable">新游戏</string>
<string name="exit_lable">退出游戏</string></resources>
3.src/com.example.helloandroid/MainActivity.java
package com.example.helloandroid;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}
然后开始添加点击游戏说明按钮出现新的activity显示游戏说明文字功能,书上是这样说的,不明白,求大神解释。书中步骤如下:
1.新建一个布局视图,用来显示游戏说明文字:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView
android:id="@+id/gametext_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gametext_text"
</ScrollView>
2.修改res/values/string.xml文件:
<string name="about_title">about Android Sudoku</string>
<string name="about_text">\
Sudoku is a logic-based,askldjflkasdjflaskjfiowerklfnbdkjdflkgjioerdfkllxxxxxxxxxxxx
</string>
3.新建about.java文件用来填充新的窗口,这步可以看懂。但居然我不知道怎样新建这样的一个文件,自己都不好意思了。大神指教。
package com.example.helloandroid;import android.app.Activity;
import android.os.Bundle;protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
4.补充src/com.example.helloandroid/MainActivity.java文件,用来把游戏说明按钮和用来显示游戏说明的activity连接起来,起到点击游戏说明就触发一个事件调用显示游戏说明的activity。这一步中好多不懂的地方,真心恳求大神们解释下。一个是在向四个按钮添加侦听的代码中几个this到底是this的什么东西(view aboutButton = this.findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);)。还有实现接口的文件也让我云里雾里,这个接口文件实现的什么功能。可以讲解下这个文件吗?package com.example.helloandroid;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set up click listeners for all the buttons
view continueButton = this.findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
view newButton = this.findViewById(R.id.new_button);
newButton.setOnClickListener(this);
view aboutButton = this.findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
view exitButton = this.findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public class MainActivity extends Activity implements OnClickListener{
public void onClick(View v){
switch (v.getId()){
case R.id.about_button;
Intent i = new Intent(this,About.class);
startActivity(i);
break;
//More buttons go here(if any)...
}
}
}
5.最后一步是在androidManifest.xml文件中注册新的activity。就不贴代码了。
真心恳求大神们详细讲解下。分不多了,只有三十分,真的谢谢大神们了!!!
解决方案 »
- 感应器问题!
- 已经有一个在顶部的Actionbar,怎样动态添加一个下面的actionbar?
- android HttpPost
- EditText被弹出来的输入框挡住
- 一个普通的apk安装到哪个文件夹下面?
- ListView加入Viewpager问题
- android中线程的handler能不能更新Activity的UI组件?
- TranslateAnimation移动View后,只有视图改变了,坐标还是原先的,怎么解决。
- 全屏和非全屏切换
- 利用Android手机相机拍照,确定照片中有多少圆形(用于计量管材数量)
- 如何在android中某个activity中添加一个按钮调用底部菜单tabhost的方法。
- android开发的sdcard权限问题和一个很诡异的错误,调试了3个小时......要疯了,真心没有招了,拜托大家好心帮帮我吧
强烈推荐你去看一本入门书籍,市面上很多,其实讲的都差不多。
当你一点一点读明白了,你就会发现5个字儿,那都不叫事儿。
先去做一个最简单的例子吧。
就是一个button,点完之后跳到第二个页面。
然后好好看看Java。。你就知道this关键字是什么了,当初我也困惑很久。
慢慢来。
你的about.java activity不是用系统建的吧,是自己建class然后改的代码吧?
你只实现了Activity的一个onCreate生命周期,而没有建成一个activity。
通过右键点击包,new一个新的android activity。系统会给你配好。
你要点击,首先要声明被点击的button。
然后对这个button设置被点击事件监听
然后再实现点击事件。
点击跳转事件,就是声明一个新的意图(意图是Intent)
意图从当前activity指向跳转的activity.class
然后启动该意图
这样就能实现跳转了。
说的我都乱了。。果然还是不适合说教。
但是你犯的错误都是不该犯的,你要好好理解,要不以后问题会更多。
2、建立新的Activity的方法如下:(最好让开发工具帮助你自动生成Actvity)
a.在你com.example.helloandroid这个包上,点击右键-》new-》Other-》出现如下界面在输入框内输入activity,出现如下界面:然后下一步下一步,设置好Activity的名称和布局文件的名称,如下图所示:创建完毕后,会出现Activity文件和XML布局文件,如下所示:然后打开AndroidManifest配置文件,将新生成的AboutActivity的配置修改下,删除如下标签的内容:
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
这个是用来指定程序启动后,首先启动那个Activity。
b.界面跳转
首先获得按钮控件
Button bt = (Button) findViewById(R.id.bt);
然后为按钮控件设置监听,并实现跳转:
bt.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
//第一个参数是本Activity,第二个是要跳转的Activity
intent.setClass(HelloworldActivity.this, About.class);
startActivity(intent);
}
});
我也是初学者,如有不妥,请指教。