我的应用布局中有两个按钮& 1 EditText ...
我希望使用Plus按钮增加EditText的文本大小。使用减号按钮减少...... 但是当我单击减号按钮而不是减小尺寸时,它会增加尺寸......
这是我的代码 -
plus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
float org_size = ed.getTextSize();
Log.d("Size", "Original Float Value" + org_size);
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources() .getDisplayMetrics());
float new_size = org_size + pixels;
if(org_size >= 90f){
Toast.makeText(MainActivity.this, "That's the Max Size available!" , Toast.LENGTH_SHORT).show();
}
else{
ed.setTextSize(new_size);
}
}
});
minus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
float org = ed.getTextSize();
Log.d("Size", "Original Float Value" + org);
float pix = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources() .getDisplayMetrics());
float snew = org - pix;
if(org <= 30f){
Toast.makeText(MainActivity.this, "That's the Minimum Size available!" , Toast.LENGTH_SHORT).show();
}
else{
ed.setTextSize(snew);
}
}
});
答案 0 :(得分:1)
请更改您的代码并再次查看,
minus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
float org = ed.getTextSize();
Log.d("Size", "Original Float Value" + org);
float scaledDensity = getApplicationContext().getResources().getDisplayMetrics().scaledDensity;
float sp = org/scaledDensity;
float pix = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources() .getDisplayMetrics());
float snew = sp - pix;
Log.d("Size", "snew" + snew);
if(org <= 30f){
Toast.makeText(MainActivity.this, "That's the Minimum Size available!" , Toast.LENGTH_SHORT).show();
}
else{
ed.setTextSize(snew);
}
}
});