Java 以编程方式在drawable下创建editText
我有一个editText:@+id/editText1,我想在onClick方法之后在R.drawable.sqwhite下面实例化它 我的问题是,drawable不是通过XML创建的,我不确定如何让editText1直接显示在R.drawable.sqwhite下面 爪哇: XML:Java 以编程方式在drawable下创建editText,java,android,xml,android-edittext,android-drawable,Java,Android,Xml,Android Edittext,Android Drawable,我有一个editText:@+id/editText1,我想在onClick方法之后在R.drawable.sqwhite下面实例化它 我的问题是,drawable不是通过XML创建的,我不确定如何让editText1直接显示在R.drawable.sqwhite下面 爪哇: XML: 截图: 您能提供添加新图像到屏幕(resourceId)的代码吗?完成-如果有任何问题,请告诉我!我理解你的代码有点困难。在XML文件中,为什么在id为editText1的EditText上方有一个没有id的
截图:
您能提供
添加新图像到屏幕(resourceId)
的代码吗?完成-如果有任何问题,请告诉我!我理解你的代码有点困难。在XML文件中,为什么在id为editText1
的EditText上方有一个没有id的ImageView?这是您希望位于EditText上方的ImageView吗?您可以删除它-它目前不起任何作用(这是我尝试过的一些疑难解答遗留下来的)。在这种情况下,我实际上会保留ImageView并在XML文件中为其分配一个id,在XML文件中指定ImageView应位于EditText上方,然后从java代码中以编程方式更改此ImageView中的drawable/image。
public void onClickAddImage(View v) {
addNewImageToScreen();
}
public void addNewImageToScreen() {
int resourceId = R.drawable.sqwhite;
addNewTextToScreen();
int m = mImageCount % 3;
if (m == 1)
resourceId = R.drawable.sqdrk;
else if (m == 2)
resourceId = R.drawable.sqwhite;
addNewImageToScreen(resourceId);
}
private void addNewTextToScreen() {
// TODO Auto-generated method stub
et.setVisibility(View.VISIBLE);
if (isErase) {
tx.setText(et.getText().toString());
} else {
tx.setText("");
et.setVisibility(View.GONE);
}
isErase = !isErase;
}
public void addNewImageToScreen(int resourceId) {
if (mLastNewCell != null)
mLastNewCell.setVisibility(View.GONE);
et.setVisibility(View.VISIBLE);
FrameLayout imageHolder = (FrameLayout) findViewById(R.id.image_source_frame);
if (imageHolder != null) {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
Gravity.CENTER);
ImageCell newView = new ImageCell(this);
newView.setImageResource(resourceId);
imageHolder.addView(newView, lp);
newView.mEmpty = false;
newView.mCellNumber = -1;
mLastNewCell = newView;
mImageCount++;
// Have this activity listen to touch and click events for the view.
newView.setOnClickListener(this);
newView.setOnLongClickListener(this);
newView.setOnTouchListener(this);
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.example.project.DragLayer xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
android:id="@+id/drag_layer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1.0" >
<GridView
android:id="@+id/image_grid_view"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.8"
android:background="@color/grid_background"
android:gravity="center"
android:horizontalSpacing="2dip"
android:numColumns="@integer/num_columns"
android:stretchMode="columnWidth"
android:verticalSpacing="2dip" />
<LinearLayout
android:id="@+id/bottom_part"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black"
android:layout_centerHorizontal="true"
android:layout_weight="0.2"
android:orientation="horizontal"
android:weightSum="1.0" >
<Button
android:id="@+id/button_add_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="onClickAddImage"
android:text="Add image" />
<com.example.project.DeleteZone
android:id="@+id/delete_zone_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="@drawable/delete_zone" />
<FrameLayout
android:id="@+id/image_source_frame"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="0.5" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:textColor="@android:color/white"
android:text="" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.example.project.DragLayer>