如何将textview准确放置在Eclipse for Eclipse中用户Long Presses的位置

时间:2015-01-21 02:11:31

标签: java android eclipse

我正在学习Eclipse for Eclipse中的代码。我用google搜索并了解View.OnLongClickListener可用于在用户长按应用程序时调用方法。但是,我很难找到一个解决方案,让TextView根据用户长按的确切位置进行移位。

我已经实施了检查用户长按的条件。有关如何使TextVIew出现在用户长按的位置的任何想法?我希望TextView准确显示用户长按的位置。

findViewById(R.id.myimage).setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            showNameInCustomPosition();// Method to call to when user long presses
            return false;
        }
    });

更新:我没有让文本移动,但是当我点击屏幕边缘时,文本会部分显示出超出屏幕范围。谁能建议如何避免这种情况?这是我的布局属性。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myimage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/shrinivas"
    android:onClick="onClick"
    android:clickable="true"
    tools:context="com.example.hello.MainActivity" >

    <TextView
        android:id="@+id/shownamecenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_text2" />

        <TextView
        android:id="@+id/shownamecustom"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_gravity="fill"
        android:text="@string/my_text" />




</RelativeLayout>

id为customcustom的那个是导致问题的那个。

1 个答案:

答案 0 :(得分:1)

试试这个,下面的代码是MainActivity 实现OnTouchListener

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends Activity implements OnTouchListener {

TextView move_textview;
RelativeLayout mainLayout;
int x,y; //used to get coordinates

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainLayout = (RelativeLayout)findViewById(R.id.mainlayout);
    move_textview = (TextView)findViewById(R.id.textView1);
    mainLayout.setOnTouchListener(this);//listener to get touch coordinates
    mainLayout.setOnLongClickListener(new OnLongClickListener() { //listen when user long click

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            setAbsoluteLocation(move_textview,x,y); //passing textview, x & y param to the method where the textview wanna move
            return false;
        }
    });

}
//methode use to move the text view
private void setAbsoluteLocation(TextView tview, int x, int y) 
{
    RelativeLayout.LayoutParams alp = (RelativeLayout.LayoutParams) tview.getLayoutParams();

    alp.leftMargin = x-50;
    alp.topMargin = y-50;
    tview.setLayoutParams(alp);

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

//Overide onTouch method used to get touch coordinates  
@Override
public boolean onTouch(View arg0, MotionEvent event) {
    // TODO Auto-generated method stub
    x = (int)event.getRawX();
    y = (int)event.getRawY();
    return false;
 }
}

activity_main.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sample.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="86dp"
    android:layout_marginTop="171dp"
    android:text="Movable" />

</RelativeLayout>