2016-08-14 2 views
1

Xamarin을 사용하여 C#으로 작성된 Android 앱이 있습니다. 필자는 배너 광고용 TextView와 Google admod AdView가 포함 된 LinearLayout으로 앱을 축소했습니다.앱 이력서에 소프트 키보드 숨기기

소프트 키보드가 앱에 표시되는 것을 원하지 않습니다. 필요하지 않습니다. 응용 프로그램을 시작하면 TextView와 배너 추가가 나타납니다. 그런 다음 다른 앱으로 전환 한 다음 첫 번째 앱으로 다시 전환하면 첫 번째 앱이 다시 시작 되 자마자 소프트 키보드가 팝업됩니다. 이는 AdView가있는 경우에만 발생합니다. 앱에서 AdView를 제거하면 소프트 키보드가 표시되지 않습니다.

앱이 처음 시작되고 AdView가있는 경우 키보드가 작동하지 않습니다. 앱이 일시 중지되었다가 다시 시작된 경우에만 작동합니다. 나는 AdView가 포함 된 경우

protected override void OnResume() 
{ 
    base.OnResume(); 
    InputMethodManager im = (InputMethodManager)GetSystemService(Context.InputMethodService); 
    im.HideSoftInputFromWindow(Window.DecorView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None); 
}         

하지만 키보드는 여전히 이력서에 팝업 :

나는 다음과 같은 OnResume 오버라이드 있습니다.

나는 특히 AdView가에 대한 OnResume에서 키보드를 숨기고 시도 :

im.HideSoftInputFromWindow(adView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None); 

을하지만 그 중 하나가 도움이되지 않았다.

나는 많은 다른 많은 stackoverflow 질문에서 찾을 수있는 모든 제안을 시도했다. AdView가있을 때 앱이 다시 시작될 때를 제외하고 모든 상황에서 키보드가 나타나지 않도록 할 수 있습니다.

내가 곤혹 스럽다. 어떤 아이디어?

자세한 내용은 ....

먼저 수정. TextView가 아닌 ​​LinearLayout에 EditText가 있습니다. 나는 소프트 키보드를 원하지 않지만 EditText를 편집 가능하게하고 싶습니다 (EditText에서 텍스트를 추가/삭제/변경하기위한 버튼을 제공하는 앱의 전체 버전에서).

나는 EditText와 AdView가 포함 된 하나의 LinearLayour로 앱을 축소했습니다. AdView에 광고를로드하고 앱을 실행 한 후 다른 앱으로 전환 한 다음 다시 전환하면 소프트 키보드가 팝업됩니다. 나는 그것을 원하지 않는다. 그러나 AdView에 광고를로드하지 않고 (여전히 axml 파일에 AdView가있는 경우) 실행 한 후 전환하면 소프트 키보드가 팝업되지 않습니다.

AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App1.App1" android:versionCode="1" android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <application android:label="App1"> 
     <meta-data android:name="com.google.android.gms.version" 
       android:value="@integer/google_play_services_version" /> 
     <activity android:name="com.google.android.gms.ads.AdActivity" 
       android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
       android:theme="@android:style/Theme.Translucent" 
       android:windowSoftInputMode="stateAlwaysHidden" /> 
    </application> 
</manifest> 

Main.axml :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/layout"> 

    <EditText xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" /> 

    <com.google.android.gms.ads.AdView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/adView" 
    ads:adSize="SMART_BANNER" 
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> 
    </com.google.android.gms.ads.AdView> 

</LinearLayout> 

MainActivity.cs :

using Android.App; 
using Android.Content; 
using Android.Views; 
using Android.Views.InputMethods; 
using Android.OS; 
using Android.Widget; 
using Android.Gms.Ads; 

namespace App1 
{ 
    [Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity 
    { 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      SetContentView(Resource.Layout.Main); 

      AdView adView = FindViewById<AdView>(Resource.Id.adView); 
      var requestbuilder = new AdRequest.Builder(); 
      adView.LoadAd(requestbuilder.Build()); 
     } 


     protected override void OnResume() 
     { 
      base.OnResume(); 

      InputMethodManager im = (InputMethodManager)GetSystemService(Context.InputMethodService); 

      im.HideSoftInputFromWindow(Window.DecorView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None); 

      LinearLayout layout = FindViewById<LinearLayout>(Resource.Id.layout); 
      im.HideSoftInputFromWindow(layout.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None); 

      EditText editText = FindViewById<EditText>(Resource.Id.editText); 
      im.HideSoftInputFromWindow(editText.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None); 

      AdView adView = FindViewById<AdView>(Resource.Id.adView); 
      im.HideSoftInputFromWindow(adView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None); 

     } 
    } 
} 

코드를 위와 같이 다음

소스는 내 프로를 보여 주다. 흠. 앱을 휴대 전화에 배포하고 앱을 실행 한 다음 다른 앱으로 전환 한 다음 다시 전환하면 첫 번째 앱을 다시 시작할 때 소프트 키보드가 팝업됩니다.

adView.LoadAd(requestbuilder.Build()); 

을 등 배포를 반복하고, 소프트 키보드가 팝업하지 않습니다하지만 ... MainActivity.cs에 다음 코드 줄을 주석 처리합니다.

내가 말했듯이, 나는 혼란 스럽다. editchext에 focuschange 처리기를 추가하여 키보드를 내려 보았습니다. 그러나 EditText가 포커스를 잃었을 때만 호출 할 수 있습니다.

답변

0

문제는 EditText이며, 활동이 생성 될 때 자동 포커스가 발생합니다. 이 기본 동작을 제거하려면이 두 행을 루트 요소에 추가하십시오.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/layout" 
    android:descendantFocusability="beforeDescendants" 
    android:focusableInTouchMode="true"> 
+0

그건 작동합니다! 고맙습니다. 하나 이상의 요구 사항이 있습니다 ... editText를 터치하면 키보드가 팝업되는 것을 원하지 않지만 touch 이벤트를 처리하려고합니다. editText에 텍스트가있을 것이므로 수정할 수 있기를 원하기 때문에 커서가 터치에 반응하도록하고 싶지만 소프트 키보드가 아닌 편집 전용 버튼을 사용하고 싶습니다. 어떤 아이디어? – JeffR

+0

또 다른 완전히 다른 질문에 감사드립니다. 나는 새로운 질문을 열 것을 제안한다. 나는 기쁘다. 내 대답을 옳은 것으로 표시하십시오. 고맙습니다. – jzeferino

+0

감사합니다. 답변으로 표시되었습니다. 여기에 새로운 질문 http://stackoverflow.com/questions/38956548/hide-soft-keyboard-for-edittext-focus-touch – JeffR

관련 문제