安卓基础开发
Mainactivity.java 文件
package com.example.first;
import android.net.Uri;
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.EditText;
public class MainActivity extends Activity {
private Button btn;//1创建对象
private Button btn2;
private EditText et1;
private EditText et2;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2对btn实例化
btn=(Button)findViewById(R.id.btt1);
btn2=(Button)findViewById(R.id.b2);
//创建单击事件 参数要传递匿名内部类
et1=(EditText)findViewById(R.id.username);
et2=(EditText)findViewById(R.id.password);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View arg0)
{
String username=et1.getText().toString();
String pwd=et1.getText().toString();
if(username.equals("z3")&&pwd.equals("z3")){
Intent in=new Intent(MainActivity.this,SecActivity.class);
startActivity(in);
}
//再次方法内斜a>a
//System.out.println("kkkkk");
//要成功的跳转需要用intent这个对象第一个参数是原来的类出发站跳转的泪终点站
//Intent in=new Intent(MainActivity.this,SecActivity.class);
//startActivity(in);
}
});
btn2.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent in2=new Intent();
in2.setAction(Intent.ACTION_CALL);
in2.setData(Uri.parse("tel:13567823232"));
startActivity(in2);
}
}
);
}//onCreate
}
第二个java文件
package com.example.first;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SecActivity extends Activity {
List list;
private ListView userlv;
final static int menu_index=Menu.FIRST;
final static int menu_content=Menu.FIRST+1;
final static int menu_last=Menu.FIRST+2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
userlv=(ListView)findViewById(R.id.list);
list=new ArrayList();
list.add("tom");
list.add("jerry");
list.add("jak");
list.add("marry");
list.add("join");
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
userlv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.sec, menu);
menu.add(0,menu_index,0,"首页").setIcon(android.R.drawable.btn_star);
menu.add(0,menu_content,1,"内").setIcon(android.R.drawable.btn_star);
menu.add(0,menu_last,2,"为页").setIcon(android.R.drawable.btn_star_big_off);
return true;
}
/* (non-Javadoc)
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//eturn super.onOptionsItemSelected(item);
switch(item.getItemId()){
case menu_index:
System.out.println("menu_index");
return true;
case menu_content:
System.out.println("menu_content");
return true;
case menu_last:
System.out.println("menu_last");
return true;
default: return false;
}
}
}
控件文件
<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" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_toRightOf="@+id/textView1"
android:text="user login" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="27dp"
android:layout_marginLeft="14dp"
android:text="打个电话问问" />
<Button
android:id="@+id/btt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/b2"
android:layout_alignLeft="@+id/textView2"
android:layout_marginBottom="62dp" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btt1"
android:layout_alignLeft="@+id/b2"
android:layout_marginBottom="48dp"
android:layout_marginLeft="16dp"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btt1"
android:layout_alignLeft="@+id/textView1"
android:text="mima" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/password"
android:layout_alignLeft="@+id/b2"
android:layout_marginBottom="35dp"
android:text="user name" />
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/password"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
控件文件