一个工程专题,实现点击按钮后出现一个新的activity显示一些内容。工程代码全部贴出,恳求大神赐教!!没分了,
一个工程专题,实现点击按钮后出现一个新的activity显示一些内容。工程代码全部贴出,恳求大神赐教!!没分了,只有三十分,真的恳求指导!!!
想实现点击游戏说明后出现一个新的activity显示游戏的说明文字。如图
首先先将工程的主要文件全部贴出:
1.res/layout/activity_main.xml
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。就不贴代码了。
真心恳求大神们详细讲解下。分不多了,只有三十分,真的谢谢大神们了!!!
[解决办法]
新手都是这样过来的,你的基础太差了。。。
强烈推荐你去看一本入门书籍,市面上很多,其实讲的都差不多。
当你一点一点读明白了,你就会发现5个字儿,那都不叫事儿。
先去做一个最简单的例子吧。
就是一个button,点完之后跳到第二个页面。
然后好好看看Java。。你就知道this关键字是什么了,当初我也困惑很久。
慢慢来。
