2

私はポートレートモードでタブレイアウトを持っています。今、私は自分のタブの中で起こったonConfigChangesに対して応答しなければなりません。グーグルの研究に続いて、私はこのコードを作っ:私のタブの中のonConfigChangesに応答するには?

のmanifest.xmlを

<uses-sdk android:minSdkVersion="7" /> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".aaa_bbb" android:label="@string/app_name" 
       android:theme="@android:style/Theme.NoTitleBar" 
       android:screenOrientation="portrait" 
       android:windowSoftInputMode="stateUnchanged|adjustPan" 
       android:configChanges="keyboardHidden"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name="aaa"> 
    </activity> 
    <activity android:name="bbb"> 
     <intent-filter> 
      <action android:name="com.mycompany.myApp.KEYBOARD" /> 
     </intent-filter> 
    </activity> 
</application> 
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 

私の主な活動:

public class aaa_bbb extends TabActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Reusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, aaa.class); 
    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("aaa"); 
    spec.setIndicator("Aaa",res.getDrawable(R.drawable.tab_aaa)); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 

    // Do the same for the other tabs 
    intent = new Intent(this, bbb.class); /* just different way to do the same job */ 
    spec = tabHost.newTabSpec("bbb"); 
    spec.setIndicator("Bbb", res.getDrawable(R.drawable.tab_bbb)); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(0); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    Toast.makeText(getBaseContext(), "Config changes", Toast.LENGTH_LONG).show(); 
    Intent i = new Intent(); 
    i.setAction("com.mycompany.myApp.KEYBOARD"); 
    sendBroadcast(i); 
} 
} 

そして最後に私のタブのクラスで(活動):

public class bbb extends Activity { 
private MyListener listener = null; 
private Boolean listener_is_registered = false; 


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

    listener = new MyListener(); 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    if (!listener_is_registered) { 
     registerReceiver(listener, new IntentFilter("com.mycompany.myApp.KEYBOARD")); 
     listener_is_registered = true; 
     Toast.makeText(getBaseContext(), "Listener registered true.", Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void onPause() { 
    super.onPause(); 

    if (listener_is_registered) { 
     unregisterReceiver(listener); 
     listener_is_registered = false; 
     Toast.makeText(getBaseContext(), "Listener registered false", Toast.LENGTH_LONG).show(); 
    } 
} 


// Nested 'listener' 
protected class MyListener extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // No need to check for the action unless the listener will 
     // will handle more than one - let's do it anyway 
     if (intent.getAction().equals("com.mycompany.myApp.KEYBOARD")) { 
      /* perform some action */ 
      Toast.makeText(getBaseContext(), "Intent reciewied", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
} 

私は間違っていた場所を知りませんでしたが、私のbbb活動はキーボードの隠しや表示に対応していません。

答えて

1

あなたは向きを正しく理解していません。あなたは特定の方向に行った後、またはキーボードを隠すだけでいくつかの他のアクションをしたいですか?これを覚えておくとブーリアンを追加すると、おそらく助けになるだろう。

public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 


     isHorizontal=true; 

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 

     isVertical=true; 
    } 
    } 
関連する問題