在我的应用程序中,微调器改变了两个TextView之间的距离。选择第一个项目时,两个TextView之间的距离为60 mm。当您选择第二项时,距离增加到70mm。
问题如下: 当我创建一个Toast以响应每个项目的选择(它只是用于检查监听器的操作)时,应用程序正常工作并完成它应该做的事情。
然而,当我删除吐司时,代码停止正常运行:当我选择第一项时,没有任何反应,当我选择第二项时,第一项的代码起作用,当我选择第三项时(否)代码)第二项的代码变为活动状态。
这是我的代码:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
index = parent.getSelectedItemPosition();
switch (index){
case 0:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT );
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 60,
r.getDisplayMetrics());
params.setMargins((int) px, 0, 0, 0);
tv2.setLayoutParams(params);
//Toast.makeText(getApplicationContext(), "posicion: "+position, Toast.LENGTH_LONG).show();
break;
case 1:
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT );
Resources r1 = getResources();
float px1 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 70,
r1.getDisplayMetrics());
params1.setMargins((int)px1,0,0,0);
tv2.setLayoutParams(params1);
//Toast.makeText(getApplicationContext(), "posicion: "+position, Toast.LENGTH_LONG).show();
break;
case 2:
//Toast.makeText(getApplicationContext(), "posicion: "+position, Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
我该如何解决?
答案 0 :(得分:0)
我想不出任何会导致上述代码停止工作的东西,只需简单地注释掉Toasts。你确定只是简单地评论它们可以解决这个问题吗?
答案 1 :(得分:0)
尝试如下,它也可以这样工作。
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT );
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 60,
r.getDisplayMetrics());
params.setMargins((int) px, 0, 0, 0);
tv2.setLayoutParams(params);
//Toast.makeText(getApplicationContext(), "posicion: "+position, Toast.LENGTH_LONG).show();
break;
case 1:
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT );
Resources r1 = getResources();
float px1 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 70,
r1.getDisplayMetrics());
params1.setMargins((int)px1,0,0,0);
tv2.setLayoutParams(params1);
//Toast.makeText(getApplicationContext(), "posicion: "+position, Toast.LENGTH_LONG).show();
break;
case 2:
//Toast.makeText(getApplicationContext(), "posicion: "+position, Toast.LENGTH_LONG).show();
Log.e("Position iss----->",String.valueOf(position));
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
答案 2 :(得分:0)
OP解决方案。
我通过将每个项目的代码放在Runnable上来解决问题。
现在的代码如下:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
tv2.postDelayed(new Runnable() {
public void run() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT );
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 60,
r.getDisplayMetrics());
params.setMargins((int) px, 0, 0, 0);
tv2.setLayoutParams(params);
}
}, 300);
break;
case 1:
tv2.postDelayed(new Runnable() {
public void run() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT );
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 70,
r.getDisplayMetrics());
params.setMargins((int) px, 0, 0, 0);
tv2.setLayoutParams(params);
}
}, 300);
break;
case 2:
tv2.postDelayed(new Runnable() {
public void run() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT );
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 30,
r.getDisplayMetrics());
params.setMargins((int) px, 0, 0, 0);
tv2.setLayoutParams(params);
}
}, 300);
break;
}
}