package com.example.zhuanhuan
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.IOException
import com.example.zhuanhuan.MainActivity
import com.example.zhuanhuan.R
import com.example.zhuanhuan.KkkActivity
import android.os.Bundle
import android.os.Environment
import android.app.Activity
import android.content.Intent
import android.view.Menu
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Button zhuceButton = (Button) findViewById(R.id.button2)
Button dengluButton = (Button) findViewById(R.id.button1)
final EditText yonghumingEditText = (EditText) findViewById(R.id.editText1)
final EditText mimaEditText = (EditText) findViewById(R.id.editText2)
zhuceButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent()
intent.setClass(MainActivity.this, KkkActivity.class)
startActivity(intent)
}
}
dengluButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory().getPath()+"/"+ yonghumingEditText.getText().toString())
byte[] input = new byte[fis.available()]
while(fis.read(input) != -1 )
fis.close()
//Toast.makeText(getApplicationContext(), new String(input), Toast.LENGTH_SHORT).show()
String mimaString = new String(input)
if (mimaString.equals(mimaEditText.getText().toString())==true) {
Toast.makeText(getApplicationContext(), "成功登陆", Toast.LENGTH_SHORT).show()
Intent intent = new Intent()
intent.setClass(MainActivity.this, QqqActivity.class)
startActivity(intent)
}
else {
Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show()
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
})
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menuthis adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu)
return true
}
}
二、
package com.example.zhuanhuan
import java.security.PublicKey
import android.os.Bundle
import android.app.Activity
import android.content.Intent
import android.view.Menu
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.Toast
public class QqqActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.qqq)
Button fanhuiButton = (Button) findViewById(R.id.button2)
Button jianceButton = (Button) findViewById(R.id.button1)
jianceButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show()
Intent new = new Intent()
intent.setClass(QqqActivity.this, MainActivity.class)
startActivity(intent)
}
})
fanhuiButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent()
intent.setClass(QqqActivity.this, AaaActivity.class)
startActivity(intent)
}
})
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menuthis adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.qqq, menu)
return true
}
}
三、
package com.example.zhuanhuan
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import com.example.zhuanhuan.MainActivity
import com.example.zhuanhuan.R
import android.os.Bundle
import android.os.Environment
import android.app.Activity
import android.content.Intent
import android.view.Menu
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.EditText
public class KkkActivity extends Activity {
private File writeFile
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.kkk)
Button backButton = (Button) findViewById(R.id.button1)
final EditText yonghumingEditText = (EditText) findViewById(R.id.editText1)
final EditText mimaEditText = (EditText) findViewById(R.id.editText2)
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
File writeFile = new File(Environment.getExternalStorageDirectory().getPath(), yonghumingEditText.getText().toString())
if (!writeFile.exists()) {
try {
writeFile.createNewFile()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
String abcString = mimaEditText.getText().toString()
FileOutputStream fos
try {
for = new FileOutputStream(writeFile)
fos.write(abcString.getBytes())
fos.flush()
fos.close()
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
Intent intent = new Intent()
intent.setClass(KkkActivity.this, MainActivity.class)
startActivity(intent)
}
})
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menuthis adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.kkk, menu)
return true
}
}
四、
package com.example.zhuanhuan
import android.os.Bundle
import android.app.Activity
import android.content.Intent
import android.view.Menu
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
public class AaaActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.aaa)
Button jianceButton = (Button) findViewById(R.id.button1)
jianceButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent()
intent.setClass(AaaActivity.this, QqqActivity.class)
startActivity(intent)
}
})
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menuthis adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.aaa, menu)
return true
}
}
A.创建一个android项目
B.创建一个新Activity
右键点击Eclipse左边(默认)你要加入Activity的包,比如,我的包是com.INdroid.layout。然后选New->Class,输入类名后(注意首字母大写),在Superclass的Browse那里点击。最后在Choose a type那薯渗里输入Activity。然后点OK就可以了.
C. 创建Activity布局
在rec/layout文件夹里创建布局xml页面 new->others->android->android xml file
D. 注册创建的activity到AndroidManifest.xml
运用eclisps可以可视化添加,双击AndroidManifest.xml 选择标签application->add->activity ,输入刚才创建 的类名。
也可以手工添加,在</activity>州旦标签下面加入
<activity android:name=".新Activity的名字"
android:label="新Activity的标题">
</activity>
这里需要注意的是,android:name="."这里有个点,这个点表示这个Activity在我所选择的包com.INdroid.layout下。
5) 在新Activity中加入onCreate方法并且绑定layout。
关于Activity的跳转问题,要补充一点,按钮绑定setOnClickListener必须放在新建OnClickListener对象之后,不然不能跳转。
到此,一个可以使用的activity即创建完成了,下一步是添加UI元素及实现页面响应代码
下面以添加一个checkbox为例说明,实现的效果是点击主Activity界面,跳转到CheckboxActivity 界面
1、向主Activity界面添加一个button。打开main..xml,使用outline窗口的“+”,添加一个button,填写button的属性
2、 在src/ActivityMain类,即主Activity的界面响应类中实现页面跳转代码,
2.1 声明 button 变量册手扰 和 监听器 OnClickListener
2.2 实现 OnClickListener监听器的跳转逻辑,即onClihck 函数
2-3 绑定 监听器OnClickListener 到 button 中
3、向checkboxActivity界面添加一个 checkbox 的 widget
4、向checkboxActivity界面添加呈现代码
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)