java安卓:如何使用类中活动的用户输入
我正在尝试创建一个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);
}
});
}
扩展视图的抽屉编辑器类
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);
}
}
# 1 楼答案
通过intent将数据传递给leveleditor
通过获取leveleditor中的日期
将setter方法添加到抽屉编辑器 然后画出来:)
公共类抽屉编辑器扩展视图{ int屏幕高度,屏幕宽度; 浮动x,y; int高度、宽度
# 2 楼答案
将值从网格输入活动传递到级别编辑器,如下所示:
并在级别_编辑器中获取如下值:
在抽屉编辑器类中创建一个方法以接受高度、重量值。调用方法将这些值传递给DraweEditor
比方说
像这样调用DrawEditor类对象上的initValues
最后,在代码中要使用的位置引用这些mHeight、mWidth值
希望这对你有帮助