【问题标题】:how to get string from EditText to Custom View如何从 EditText 获取字符串到自定义视图
【发布时间】:2017-05-19 17:21:01
【问题描述】:

我一直在活动中处理一些自定义View。现在假设我们在该活动中也有EditText。我希望我的自定义View 在我触摸View 时获得EditText 中的值(这意味着我希望在onTouch 方法中完成此操作)。问题是,我一直在单独的班级上处理我的自定义View,但我找不到完成这项工作的方法。我最初想过使用findViewbyId 方法来获取id,但它似乎无法撤消。因此,非常感谢任何帮助或想法。

以下是我的项目的一些代码:

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_nono"
    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:orientation="vertical"
    tools:context="snacker.nonogramsolver.NonoActivity">

    <snacker.nonogramsolver.Nono /* MY CUSTOM VIEW */
        android:id="@+id/NonoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        ... /*some elements are here, not important for this question*/

    <EditText
        android:id="@+id/line_input"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
</LinearLayout>

我的自定义查看 java 文件:

package snacker.nonogramsolver;

import ...;

public class Nono extends View implements View.OnTouchListener{
... /* some variables */

    public Nono(){
        super(null);
    };

    public Nono(Context context){
        super(context);
        //initializeBoard(10,10);
        mCellWidth = mCellHeight = this.getWidth() / (mWidth);
    }

    public Nono(Context context, AttributeSet attrs){
        super(context, attrs);
        ...}

    ... /* SOME CODES ARE HERE, NOT IMPORTANT FOR THIS QUESTION */

    public boolean onTouch(View v, MotionEvent event){
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            mXNow = getCol(event.getX());
            mYNow = getRow(event.getY());
            if(mYNow == -1 && mXNow != -1){
                /* THIS IS WHERE I WANT THIS METHOD TO GET VALUE FROM EDITTEXT */
            }
            invalidate();
            return true;
        }
        return false;
    }

还有我的 Activity java 文件:

public class NonoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ;

        setContentView(R.layout.activity_nono);
        Intent intent = getIntent();
        int width = intent.getIntExtra("width", 5);
        int height = intent.getIntExtra("height", 5);

        Button btn = (Button)findViewById(R.id.btn_clear);
        Nono sdk = (Nono)findViewById(R.id.NonoView);



        sdk.setOnTouchListener(sdk);
        sdk.initializeBoard(width,height);
        btn.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                Nono sdk = (Nono)findViewById(R.id.NonoView);
                sdk.initializeBoard(sdk.mWidth,sdk.mHeight);
            }
        });

    }
}

【问题讨论】:

  • 应该由sdk.findViewById(R.id.line_input)工作
  • Nono 的布局是什么?
  • @cYrixmorten 已解决。嗯,这很尴尬。我也没有想过在这里使用 R.id。但是,你能告诉我为什么我也可以在这里使用 R 吗? R 是否包含此包中的所有 id?我对 android 很陌生,所以我认为 R 被用作本地。
  • @FlorescuGeorgeCătălin 我的 Nono 视图没有布局,只有一个 Canvas 可供绘制。
  • 然后,当您想在画布上设置该文本时,将其保存到 Nono 类中的私有 String 中。

标签: java android android-custom-view


【解决方案1】:

感谢 cYrixmorten 解决。

我可以使用findViewById(R.id.line_input) 方法。我没有想过在这里使用R.id

因此我的代码应该是这样的:

public boolean onTouch(View v, MotionEvent event){
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            mXNow = getCol(event.getX());
            mYNow = getRow(event.getY());
            if(mYNow == -1 && mXNow != -1){
                EditText sdk = (EditText)findViewById(R.id.line_input);
                String linetext = sdk.getText().toString();
                /* continue my work */
            }
            invalidate();
            return true;
        }
        return false;
    }

再次感谢。

编辑:似乎findViewById 在使用同级视图的 ID 调用时返回 null。所以看起来我必须实现外部方法来获取id,而不是直接在CustomView中使用findViewById

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2013-08-20
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    相关资源
    最近更新 更多