【问题标题】:Moving from one activity to another - android从一项活动转移到另一项活动 - android
【发布时间】:2011-01-19 06:39:07
【问题描述】:

我在 android 中有以下屏幕设置。单击屏幕1上的一个按钮,您将被带到屏幕2。 Screen2 在屏幕底部有 2 个按钮。一个按钮显示可搜索的 listView,另一个按钮将您带回 screen1。 screen1的代码:

screen1.java  

package com.example.screenchange;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class ScreenChange extends Activity {
    /** Called when the activity is first created. */

    private Button button01;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button01 = (Button)findViewById(R.id.button01);
        button01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.v(this.toString(), "Inside on click listener for screen 2.");
                Intent intent = new Intent(v.getContext(), screen2.class);
                Log.v(this.toString(), "Intent created. Moving to start activity.");
                startActivity(intent);
            }
        });
    }
}   

screen1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <Button
        android:id="@+id/button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Move to screen 2"
        />

</LinearLayout>  

Screen2.java:

package com.example.screenchange;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class screen2 extends ListActivity {

    private Button button02;
    private Button button03;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);

        button02 = (Button)findViewById(R.id.button02);
        button02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.v(this.toString(), "Button02 clicked.");
                Intent intent = new Intent();   //takes control back to screen1.
                finish();
            }
        });

        button03 = (Button)findViewById(R.id.button03);
        button03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.v(this.toString(), "Button03 clicked.");

                ArrayAdapter<String> listView = new ArrayAdapter<String>(v.getContext(), R.layout.listview, COUNTRIES);
                setListAdapter(listView);
                getListView().setTextFilterEnabled(true);

            }
        });
    }

    static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
        "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
        "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
        "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
        "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
        "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
        "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
        "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
        "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
        "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
        "Cook Islands"
}  

screen2.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="Welcome to screen 2">

  <Button
    android:id="@+id/button02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get back to screen 1."
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:clickable="true">
  </Button>

  <Button
    android:id="@+id/button03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Display countries."
    android:clickable="true">
  </Button>

  <ListView
    android:id="@+android:id/android:list"
    android:layout_height="fill_parent"
    android:layout_width="wrap_content"
    android:text="@id/text1">
  </ListView>   

</RelativeLayout>  

listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/text1">
</TextView>  

机器人清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.screenchange"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ScreenChange"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".screen2"
            android:label="Screen 2 - New actvity.">
        </activity>
    </application>


</manifest>   

我遇到的问题如下:
1. 切换到screen2时,screen2上的按钮没有反应。 logcat 上没有输出,什么也没有。
2. 显示列表视图时甚至可能出现错误,但我看不到任何错误。

欢迎任何帮助,
斯里拉姆。

【问题讨论】:

  • 嗨,Sriram,我在下面发布的答案有什么问题吗?
  • @HellBoy:不,没有。您发布的代码运行良好。我只需要对其进行一些补充以确保按钮也具有焦点。我在对您的回复的评论中提到了这一点。
  • 您好,我按原样尝试了此示例,但应用程序强制关闭?我使用了 Saurabh 的建议,我做错了什么吗?谢谢
  • @sunny:你尝试了什么?我希望你正确地复制了所有内容。

标签: android listview android-activity


【解决方案1】:

我复制了你的代码 eclipse 并测试了。

screen2.xml:文件

  <ListView
    android:id="@+android:id/android:list"
    android:layout_height="wrap_content"// instead of fill_parent  
    android:layout_width="wrap_content"
    android:text="@id/text1">
  </ListView> 

您的代码将起作用。

但是您单击“显示国家/地区”,您的列表视图会填满整个屏幕。而且您的按钮再次无法点击。我的建议是给你的列表视图一些最大尺寸,它应该占据多少,以便你的按钮保持可点击状态。

我猜由于列表视图中的 fill_parent,您的按钮可能无法获得焦点。

【讨论】:

  • 做到了。再加上 android:layout_above="@id/button03" 也使列表视图保持在按钮上方,并且按钮本身处于焦点。非常感谢地狱男孩!
【解决方案2】:

我复制并测试了上面的代码,但应用程序一直在强制关闭,直到在 ScreenChange 中进行以下更改

 
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen1); // instead of R.layout.main

【讨论】:

    【解决方案3】:

    将您的 button2 代码更改为此

    button02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.v(this.toString(), "Button02 clicked.");
                finish();
            }
        });
    

    无需创建新的意图,finish() 即可完成工作

    【讨论】:

      猜你喜欢
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      相关资源
      最近更新 更多