EditText仅在我单击ImageButton时出现

时间:2017-12-15 09:25:52

标签: android android-edittext android-imagebutton

当我点击主页上的textfield时,我想在其中显示Imagebutton放置文字,我尝试了一些方法来执行此操作,但我textfield出现了serv = (ImageButton) findViewById(R.id.serverpref); serv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { txtServ = (EditText) findViewById(R.id.txtServ); } }); 像这样的时间,而不是我想要的

doInBackground()

我该怎么办?

4 个答案:

答案 0 :(得分:2)

您需要设置EditText ClickListener Imageview txtServ = (EditText) findViewById(R.id.txtServ); txtServ.setVisibility(View.GONE); serv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { txtServ.setVisibility(View.VISIBLE); } }); android:visibility="gone"以下代码

的可见性

<EditText
    android:layout_width="match_parent"
    android:visibility="gone"
    android:layout_height="wrap_content"
    />

编辑:让您的EditText可见性{{1}} inisde您的xml布局如下

{{1}}

答案 1 :(得分:2)

让它在xml中“消失”或“隐身”。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.example.android.coftest"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'org.eclipse.collections:eclipse-collections-api:9.0.0'
    compile 'org.eclipse.collections:eclipse-collections:9.0.0'
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:support-v4:26.+'
    compile 'com.android.support:support-vector-drawable:26.+'
    testCompile 'junit:junit:4.12'
    compile project(':openCVLibrary330')
}

然后在点击

上显示
<EditText
    android:layout_width="match_parent"
    android:inputType="textCapCharacters"
    android:visibility="gone"
    android:layout_height="wrap_content"
    />

阅读View了解Visibilty常数。

答案 2 :(得分:1)

试试这个会起作用。最初设置布局文件中可见性

txtServ = (EditText) findViewById(R.id.txtServ);

    serv = (ImageButton) findViewById(R.id.serverpref);
            serv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   if(txtServ.getVisibility() == View.GONE){
                        txtServ.setVisibility(View.VISIBLE)
                    } 

             }
            });

答案 3 :(得分:1)

您需要的是首先定义您的edittext

EditText txtServ = (EditText) findViewById(R.id.txtServ);

然后隐藏它:

txtServ.setVisibility(View.INVISIBLE);

然后在onClickListener中,显示它:

      serv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            txtServ.setVisibility(View.VISIBLE);
        }
    });