Android:如何使用类中活动的用户输入

时间:2015-06-30 09:00:13

标签: java android android-activity

我正在尝试创建一个Android应用程序,提示用户输入高度和宽度,然后在屏幕中间绘制一个方形到用户输入的尺寸。我的问题是我不知道如何获取用户输入的2个数字,并将它们用作drawGrid类中高度和宽度的值。有谁知道这是怎么做的,我的代码如下。

...活性

public class Grid_Input extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grid__input);

    createUsersGrid();
}

private void createUsersGrid(){
    Button btnMakeGrid = (Button) findViewById(R.id.btnMakeGrid);
    EditText editText2 = (EditText)findViewById(R.id.editText2);
    EditText editText3 = (EditText)findViewById(R.id.editText3);
    btnMakeGrid.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), Level_Editor.class);
            startActivityForResult(intent, 0);
        }
    });
}

扩展View

的DrawEditor类
public class DrawEditor extends View {
    int screenHeight, screenWidth;
    float x, y;

    public DrawEditor(Context context) {
        super(context);
        screenHeight = this.getResources().getDisplayMetrics().heightPixels;
        screenWidth = this.getResources().getDisplayMetrics().widthPixels;

    }

    public void drawGrid(Canvas canvas) {

        int height = ;    //<--HOW DO I USE THE USER INPUT TO SET HEIGHT HERE?
        int width = ;
            canvas.drawLine(newX, startY, newX, stopY, green);


    }

        @Override
    public void onDraw(Canvas canvas) {

        canvas.drawRect(screenWidth-400,screenHeight-200,screenWidth-100,screenHeight-100,red);

    }
}

2 个答案:

答案 0 :(得分:1)

通过意图将数据传递给出租人

private void createUsersGrid(){
    Button btnMakeGrid = (Button) findViewById(R.id.btnMakeGrid);
    final EditText editText2 = (EditText)findViewById(R.id.editText2);
    final EditText editText3 = (EditText)findViewById(R.id.editText3);
    btnMakeGrid.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), Level_Editor.class);
            String editText2String = editText2.getText().toString();
            String editText3String = editText3.getText().toString();
            intent.putExtra("width", editText2String);
            intent.putExtra("height", editText3String);
            startActivityForResult(intent, 0);
        }
    });
}

通过

在leveleditor中获取日期
getIntent().getStringExtra("width");
getIntent().getStringExtra("height");

将setter方法添加到DrawEditor 并绘制它:)

公共类DrawEditor扩展View {         int screenHeight,screenWidth;         float x,y;         int height,width;

    public DrawEditor(Context context) {
        super(context);
        screenHeight = this.getResources().getDisplayMetrics().heightPixels;
        screenWidth = this.getResources().getDisplayMetrics().widthPixels;

    }

    public void setHeightAndWidth(int height, int width) {
        this.height = height;
        this.width = width;
    }

    public void drawGrid(Canvas canvas) {

        if(height != 0 && width != 0) {
            canvas.drawLine(newX, startY, newX, stopY, green);
        }


    }

    @Override
    public void onDraw(Canvas canvas) {

        canvas.drawRect(screenWidth - 400, screenHeight - 200, screenWidth - 100, screenHeight - 100, red);

    }
}

答案 1 :(得分:0)

将您的值从Grid_Input活动传递给Level_Editor,如下所示:

Intent intent = new Intent(v.getContext(), Level_Editor.class);
intent.putExtra("width", editText2.getText());
intent.putExtra("height", editText3.getText());
startActivityForResult(intent, 0);

并在Level_Editor中获取这样的值:

String ht = getIntent().getStringExtra("height");
String wt = getIntent().getStringExtra("width");

在DrawEditor类中创建一个方法来接受高度,权重值。调用方法将这些值传递给DrawEditor。

让我们说

public void initValues(String height, String weight) {
        mHeight = Integer.parseInt(height);    
        mWidth = Integer.parseInt(weight);          
    }

在DrawEditor类对象上调用initValues,如下所示

 DrawEditor de = new DrawEditor();
de.initValues(ht, wt);

最后将这些mHeight,mWidth值引用到您要在代码中使用的位置。

希望这对你有帮助。