gpt4 book ai didi

java - 动态创建 View 并添加到布局并使其可编辑

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:19 25 4
gpt4 key购买 nike

我创建了一个具有自定义 TextView 的 Activity ,可以将其拖放到屏幕上任何需要的地方。我想使用从警报对话框获取到屏幕的文本动态添加此 TextView 。这是 Activity 的代码。单击菜单“添加文本”时,它将打开警报对话框,它将从用户那里获取文本。现在,当用户在警报对话框上单击“确定”时,我想在屏幕上动态创建 DragView 。请指导我如何实现这一目标。

public class DragActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drag);
Intent intent = getIntent();
int ImageId = intent.getIntExtra("drawableId", 0);
this.findViewById(android.R.id.content).setBackgroundResource(ImageId);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.drag, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_addText:
AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Add text");
alert.setMessage("Please enter text to be displayed on image");

// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();

}
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});

alert.show();
return true;

default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}



}

以及自定义 TextView 代码。

public class DragView extends TextView {

private float mLastTouchX;
private float mLastTouchY;

private float mDeltaX;
private float mDeltaY;

public DragView(Context context) {
super(context);
init();
}

public DragView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
setText("This is the text");
setTextColor(Color.BLUE);
setTextSize(20);

setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();

mLastTouchX = event.getRawX();
mLastTouchY = event.getRawY();

switch (action) {
case MotionEvent.ACTION_DOWN: {
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) getLayoutParams();
mDeltaX = mLastTouchX - lParams.leftMargin;
mDeltaY = mLastTouchY - lParams.topMargin;

break;
}
case MotionEvent.ACTION_MOVE: {
mLastTouchX = event.getRawX();
mLastTouchY = event.getRawY();

final RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
params.leftMargin = (int) (mLastTouchX - mDeltaX);
params.topMargin = (int) (mLastTouchY - mDeltaY);
setLayoutParams(params);

break;
}
}
invalidate();

return true;
}
});

setOnLongClickListener( new OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
setText("The text is changed");
return false;
}
});
}

}

XML 文件-

<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"
tools:context=".DragActivity" >

<com.example.dragview.DragView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

/>

<com.example.dragview.DragView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:ems="10" >

<requestFocus />
</com.example.dragview.DragView>

</RelativeLayout>

enter image description here

最佳答案

我可以通过以下代码解决它。

alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String value = input.getText().toString();
final DragView textView = new DragView(
getApplicationContext());
textView.setText(value);
textView.setTextColor(Color.BLUE);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(params);
relativeLayout.addView(textView, params);
}
});

alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Canceled.
}
});

alert.show();
return true;

关于java - 动态创建 View 并添加到布局并使其可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23647185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com