我搜索了很多问题,但没有找到任何合适的问题解决方案。在CanvasView类中有一个3D空间,我创建并自由移动由简单元素组成的复杂3D对象。这里我需要一个文本输入的可能性,例如使用EditText在我保存之前或加载保存的对象之前输入创建对象的名称。尝试了两种方法 - 首先如下: 主要活动:
public class Activity_Moving extends Activity {
private CanvasView customCanvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
customCanvas = (CanvasView) findViewById(R.id.signature_canvas);
}
public void saveName(View v) {
customCanvas.saveName();
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<com.javacodegeeks.androidcanvasexample.CanvasView
android:id="@+id/signature_canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:contentDescription="@string/app_name"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:ems="10"
android:hint="Input file name"
android:inputType="text"
android:visibility="visible" />
</FrameLayout>
CanvasView类 - 这里我放了一个EditText,它应该能够输入保存或加载文件时使用的文件名:
public class CanvasView extends View {
private EditText edText;
public CanvasView(Context c, AttributeSet attrs) {
super(c, attrs);
context = c;
//edText.findViewById(editText);// Here it causes error !!!!!
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
edText = new EditText(context);
edText.findViewById(editText);// Here it does not cause error.
//This is where everything is drawn
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//Here I am performing object movements and saving or loading of files
by touching certain screen coordinates.
if (x > 650)
if ((y > 420)&&(y < 490)) {saveName();}//Tady by se měl vložit název
//souboru.
}
public void saveName () {
objName = edText.getText().toString();//THIS CAUSES STOPPING OF THE APPLICATION!!!!!!!!!!!!!!
Toast.makeText(context,"The name is "+objName, Toast.LENGTH_SHORT).show();
}
}
}
即使编译完成正常,EditText也可见,并且可以在其中写入名称并且所有内容都正常工作,当激活saveName方法时,应用程序停止说:&#34;不幸的是,Moving_Element(这是应用程序的名称)已停止。&#34; (Moving_Element是应用程序的名称。)
然后是第二种方法,即当应用程序运行时,在onDraw(Canvas canvas)方法中以编程方式创建EditText:
LinearLayout lL = new LinearLayout(context);
lL.setBackgroundColor(Color.LTGRAY);
EditText editTextView = new EditText(context);
editTextView.setBackgroundColor(Color.DKGRAY);
editTextView.setVisibility(View.VISIBLE);
editTextView.setTextColor(Color.WHITE);
editTextView.setHint("AHOJ");
editTextView.setGravity(Gravity.RIGHT);//This does not affect the position
at all.
lL.addView(editTextView);
lL.measure(canvas.getWidth(), canvas.getHeight());
lL.layout(0, 0, canvas.getWidth()/2-50, canvas.getHeight()/2+50);
// placing the edit text at specific co-ordinates:
canvas.translate(0, 0);
lL.draw(canvas);
创建了LinearLayout和EditText,(为了看到它我将LayoutColor的背景色设置为LTGRAY,将EditText设置为DKGRAY,将字体设置为WHITE),写入提示但创建的EditText左上角无法移动到所需位置,更重要的是无法写入。 请让我知道任何解决方案。我更喜欢在XML文件中使用EditTest的第一种方式。谢谢!