每次拍照时,如何让editText显示在我的图像下方?

时间:2017-01-03 14:42:58

标签: android android-layout android-layoutparams

我正在创建一个记事本类型的应用程序,用户可以在文本中输入和放置图片。

在我的应用中,我有buttoneditTextimageView。启动时,只有buttoneditText。用户输入editText,当他按下button时,相机会打开。拍摄的照片放在editText下方(在用户输入的最后一行下)。在图片下方会出现一个新的editText,用户可以在其中输入文字。当他再次按下按钮时,会拍摄另一张照片,放在第二张editText下面,第三张editText可供用户输入文字。

因此,每当用户按下按钮拍照时,图像下方会出现一个新的editText。这样可以保持循环,允许用户输入文本和图像,以及他想要的任意数量的图像和段落。

但这不会发生。我只能输入一次文字并拍摄1张照片。没有第二个editText出现。这里出了什么问题。

交替editTextimageView的代码位于onActivityResult()

mainActivity.java

package com.example.nirvan.cameraexample3;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity
{


ImageView myImage;
EditText textVar;
int flag=0;             //flag==0 means place img below text, ==1 means below textVar

private String pictureImagePath = "";
Uri outputFileUri;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button imgButton=(Button) findViewById(R.id.imgButton);


    View.OnClickListener imgButtonClickListener=new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = timeStamp + ".jpg";
            File storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
            File file = new File(pictureImagePath);
            outputFileUri = Uri.fromFile(file);
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(cameraIntent, 1);
        }
    };
    imgButton.setOnClickListener(imgButtonClickListener);


}



protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    Log.d("TAG","CUSTOOOM");
    RelativeLayout relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
    myImage=new ImageView(this);
    myImage.setId(+1);




    if (requestCode == 1 && resultCode == RESULT_OK)
    {
        File imgFile = new File(pictureImagePath);
       if (imgFile.exists())
        {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
           // myImage = (ImageView) findViewById(R.id.imageViewTest);

            //this code ensures the imageView is placed below the editText
            RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

            if(flag==0)
               {
                   relativeParams.addRule(RelativeLayout.BELOW, R.id.text);
                   flag=1;
               }
            else if(flag==1)
                relativeParams.addRule(RelativeLayout.BELOW, textVar.getId());

            //myImage.setLayoutParams(relativeParams);
            relativeLayout.addView(myImage,relativeParams);
            //
            myImage.setImageBitmap(myBitmap);


            //place the new editText below the imgaeView just placed
            relativeParams = new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            relativeParams.addRule(RelativeLayout.BELOW, myImage.getId());
            textVar=new EditText(this);
            textVar.setId(+2    );
            relativeLayout.addView(textVar,relativeParams);
            textVar.setText("NEW");

        }
    }//






}



}

mainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:id="@+id/relativeLayout"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.nirvan.cameraexample3.MainActivity">





<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="IMG"
    android:id="@+id/imgButton"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text"
    android:layout_below="@id/imgButton"
    />

</RelativeLayout>

更新 - 根据Petrumo的建议,我修改了代码。虽然它仍然没有按照它应该的方式工作。拍摄第一张图像后,图像下方未显示第二张editText。     我确保idtextView的{​​{1}} s对于方法的每次迭代都是不同的。

onActivityResult()已更新

imageView

UPDATE2:所以我只是用Petrumo的代码替换protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("TAG","CUSTOOOM"); RelativeLayout relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout); myImage=new ImageView(this); myImage.setId(id1++); textVar.setId(id2++); if (requestCode == 1 && resultCode == RESULT_OK) { File imgFile = new File(pictureImagePath); if (imgFile.exists()) { Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); // myImage = (ImageView) findViewById(R.id.imageViewTest); //this code ensures the imageView is placed below the editText RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); if(flag==0) { relativeParams.addRule(RelativeLayout.BELOW, R.id.text); flag=1; } else if(flag==1) relativeParams.addRule(RelativeLayout.BELOW, textVar.getId()); //myImage.setLayoutParams(relativeParams); relativeLayout.addView(myImage,relativeParams); // myImage.setImageBitmap(myBitmap); //place the new editText below the imgaeView just placed relativeParams = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); relativeParams.addRule(RelativeLayout.BELOW, myImage.getId()); textVar=new EditText(this); //textVar.setId(id2++); relativeLayout.addView(textVar,relativeParams); textVar.setText("NEW"); } }// } 内的代码..它的工作原理。 onActivityResult s出现在另一个之下。但是,当我插入editText代码时,应用程序在拍摄后立即崩溃。

imageView

日志

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    RelativeLayout relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams
            (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    if(textVar != null)
    {
        relativeParams.addRule(RelativeLayout.BELOW, textVar.getId());
    }
    else
    {
        relativeParams.addRule(RelativeLayout.BELOW, R.id.text);
    }

    myImage=new ImageView(this);
    myImage.setId(id1++);
    //set image

        File imgFile = new File(pictureImagePath);
        if (imgFile.exists() && requestCode == 1 && resultCode == RESULT_OK)
        {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            relativeLayout.addView(myImage, relativeParams);
            myImage.setImageBitmap(myBitmap);


        }

    //



    textVar=new EditText(MainActivity.this);
    textVar.setId(id2++);
    relativeParams.addRule(RelativeLayout.BELOW, myImage.getId());
    relativeLayout.addView(textVar, relativeParams);
    textVar.setText("NEW");
    relativeLayout.addView(textVar, relativeParams);




}

1 个答案:

答案 0 :(得分:0)

使用的代码有几个问题:

  • 使用相同的id textVar.setId(+2);

这应该是

textVar.setId(ID ++);

其中id是全局变量

  • 同样需要应用于图片ID,如果你想将它放在下面,它需要是唯一的并且下一个使用

我简化了您的解决方案并使其正常运行,您可以将其用作基础并在其间添加您的图像:

 select s from example s where s.id='1234';

一种更简单的方法,如果您只想在下面添加它们,可以使用方向垂直的LinearLayout,或者更优雅的解决方案,将其更改为带有适配器的列表,如评论中所示。