android中的Activity的开启StartActivity()和StartActivityForResult()

这几天接触android程序,在程序中看见StartActivity()函数,所以在网上百度的时候就出现了和StartActivityForResult()函数的比较,个人感觉比较好,所以就按照楼主给的Demo敲了一遍。但是由于楼主是粘贴的代码,所以给出来的部分不是太全,对于刚刚接触的新手来说遇见不少的问题。


 写了一个关于开启Activity的Demo:共三个Activity:MainActivity用户填写用户姓名电话城市、CityActivity用于用户选择城市,ShowActivity用于展示用户信息。


首先我还是先把代码贴出来:

MainActivity.java:

package com.example.test;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity implements OnClickListener {
private Button button_city,button_phone,button_save;
private EditText editText_city,editText_name,editText_phone;
private static final int SELECT_CITY_REQUEST =2;
private static final int SELECT_PHONE_REQUEST =3; 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


button_city = (Button)findViewById(R.id.select_city);
button_phone = (Button)findViewById(R.id.phone1);
button_save = (Button)findViewById(R.id.save);
editText_city = (EditText)findViewById(R.id.editText_city);
editText_name = (EditText)findViewById(R.id.editText_name);
editText_phone = (EditText)findViewById(R.id.editText_phone);
       
button_city.setOnClickListener(this);
button_phone.setOnClickListener(this);
button_save.setOnClickListener(this);
  }

public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
     case R.id.select_city:
          Intent i = new Intent ();
           i.setClass(MainActivity.this, CityActivity.class);
           startActivityForResult(i, SELECT_CITY_REQUEST);
           break;
       case R.id.phone1:
           Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
           pickContactIntent.setType(Phone.CONTENT_TYPE);
           startActivityForResult(pickContactIntent, SELECT_PHONE_REQUEST);
          break;
      case R.id.save:
          String name=editText_name.getText().toString();
          String city = editText_city.getText().toString();
          String phone = editText_phone.getText().toString();
          Intent saveIntent = new Intent(MainActivity.this,ShowActivity.class);
          saveIntent.putExtra("content", "NAME: "+name+"\nCITY:   "+city+"\nPHONE:"+phone);
          startActivity(saveIntent);
            break;
     default:
          break;
     }        
}
}

对应的layout配置:

<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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TextView>

    <Button
        android:id="@+id/name1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView1"
        android:layout_marginTop="28dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="Name" />

    <Button
        android:id="@+id/city1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name1"
        android:layout_below="@+id/name1"
        android:layout_marginTop="34dp"
        android:text="City" />


    <EditText
        android:id="@+id/editText_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/city1"
        android:layout_toRightOf="@+id/name1"
        android:ems="10" >


        <requestFocus />
    </EditText>


    <EditText
        android:id="@+id/editText_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/phone1"
        android:layout_toRightOf="@+id/city1"
        android:ems="10"
        android:inputType="textPersonName" />


    <Button
        android:id="@+id/select_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText_city"
        android:layout_alignRight="@+id/editText_city"
        android:text="select city" />


    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/namephone"
        android:layout_below="@+id/phone1"
        android:layout_marginTop="46dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="save" />


    <Button
        android:id="@+id/phone1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/city1"
        android:layout_marginTop="101dp"
        android:text="select_phone" />


    <EditText
        android:id="@+id/editText_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/namephone"
        android:layout_toRightOf="@+id/namephone"
        android:ems="10"
        android:inputType="phone" />


    <Button
        android:id="@+id/namephone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/phone1"
        android:layout_marginBottom="47dp"
        android:text="phone" />
save" />
</RelativeLayout>




CityActivity.java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class CityActivity extends Activity {
private ExpandableListView eListView;


@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.layout_city);
initEListView();
}


private void initEListView() {


ExpandableListAdapter adapter = new BaseExpandableListAdapter() {


private String[] countries = new String[] { "China", "USA", "UK" };


private String[][] cities = new String[][] {
{ "BeiJing", "ShangHai", "HongKong", "TaiBei" },
{ "Washington", "New York", "Los Angles", "San Fransico" },
{ "London", "Manchester", "Birmingham", "Liverpool" } };


private TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(CityActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
textView.setPadding(0, 0, 0, 0);
textView.setTextSize(20);
return textView;


}


public boolean isChildSelectable(int groupPosition,
int childPosition) {
Toast.makeText(
CityActivity.this,
"you have clicked Group:" + groupPosition + "Child:"
+ childPosition + "\n"
+ countries[groupPosition] + "  "
+ cities[groupPosition][childPosition],
Toast.LENGTH_SHORT).show();
Intent i = new Intent();
i.putExtra("city", cities[groupPosition][childPosition]);
setResult(RESULT_OK, i);
finish();
return true;
}


public boolean hasStableIds() {
return true;
}


public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(CityActivity.this);
linearLayout.setOrientation(0);


TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
linearLayout.addView(textView);
return linearLayout;
}


public long getGroupId(int groupPosition) {
return groupPosition;
}


public int getGroupCount() {
return countries.length;
}


public Object getGroup(int groupPosition) {
return countries[groupPosition];
}


public int getChildrenCount(int groupPosition) {
return cities[groupPosition].length;
}


public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition)
.toString());
textView.setGravity(Gravity.CENTER_HORIZONTAL);
return textView;
}


public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}


public Object getChild(int groupPosition, int childPosition) {
return cities[groupPosition][childPosition];
}

};
eListView = (ExpandableListView) findViewById(R.id.eListView);
      eListView.setAdapter(adapter);

}
}


CityActivity对应的layout配置:




ShowActivity .java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;


public class ShowActivity extends Activity{
private TextView showTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_show);
showTextView = (TextView)findViewById(R.id.textView1);
      
         Intent i =getIntent();
        String content = i.getStringExtra("content");
        showTextView.setText(content);
}
}

ShowActivity 对应的layout配置文件:

<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" >
    </TextView>


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_marginLeft="62dp"
        android:layout_marginTop="42dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="TextView" />


</RelativeLayout>





运行成功后出现的画面:






其中在选择城市的界面,会有点小错误,希望有谁解决了可以跟我说一下:1143403680(qq)


在其中我出现的错误:

1:在mainactivity界面中,点击sclect_city按钮会闪退出程序,这是因为在cityactivity.java中,id号没有查找到:

eListView = (ExpandableListView) findViewById(R.id.eListView);
      eListView.setAdapter(adapter);

2:刚接触android程序,在创建了三个activity时,要在清单中(AndroidManifest.xml文件中)添加上另外的activity:


3:还有就是上面提到的,虽然在ExpandableListView控件中可以显示城市名,但是会一闪而过,目前还没有解决。



好了,基本上我遇见的问题就这几个,也许是我愚笨了点,还望大神们多多指教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值