2011-01-19 94 views
0

我在android中有以下屏幕設置。點擊screen1上的按鈕,點擊屏幕2。屏幕2在屏幕底部有2個按鈕。一個按鈕顯示可搜索的listView,另一個按鈕則將您帶回screen1。對於屏蔽1代碼:從一個活動移動到另一個 - android

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> 

Android的清單:

<?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.屏幕2的按鈕去反應遲鈍我切換到畫面2的時刻。 logcat上沒有輸出,沒有任何東西。
2.甚至可能在獲取列表視圖時出現錯誤,但我無法看到任何內容。

歡迎任何幫助,
Sriram。

+0

嗨斯利拉姆是有什麼毛病我的答案貼在下面。 – Vivek 2011-01-20 13:39:47

+0

@HellBoy:沒有沒有。您發佈的代碼運作良好。我只是做了一些補充,以確保按鈕也有焦點。我在評論中提到你的迴應。 – Sriram 2011-01-21 06:27:27

+0

嗨 我試過這個例子,因爲它是應用程序是強制關閉? 我用Saurabh的建議有什麼我做錯了嗎? 謝謝 – 2011-02-08 11:06:05

回答

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> 

您的代碼將工作。

但是你點擊了「顯示國家」你的listview填充整個屏幕。而且你的按鈕再次變得無法點擊。我的建議是,給你的listview一些最大的大小,它應該佔用多少,以便你的按鈕保持可點擊。

我猜是因爲列表視圖中的fill_parent,你的按鈕可能沒有得到重點。

0

更改按鈕2碼本

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

有)沒有必要創建一個新的意圖完成(會做的工作

1

我複製並測試上面的代碼,但應用程序一直運行到的力接近,直到下一個變化是在ScreenChange取得


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

相關問題