Android(nine)

Toast
Android中提供一种简单的Toast消息提示框机制,可以在用户点击了某些按钮后,提示用户一些信息,提示的信息不能被用户点击,Toast的提示信息根据用户设置的显示时间后自动消失。Toast的提示信息可以在调试程序的时候方便的显示某些想显示的东西。

两种方法创建Toast

第一种方法的Java代码:

makeText(Context context, int resId, int duration)

参数:context是toast显示在哪个上下文,通常是当前Activity;resId指显示内容引用Resouce那条数据,就是从R类中去指定显示的消息内容;duration指定显示时间,Toast默认有LENGTH_SHORT和LENGTH_LONG两常量,分别表示短时间显示和长时间显示。

第二种方法的Java代码:

makeText(Context context, CharSequence text, int duration)

参数context和duration与第一个方法相同,参数text可以自己写消息内容。

用上面任意方法创建Toast对象之后调用方法show()即可显示。

Java代码:

Toast toast = Toast.makeText(ToastDemoActivity.this, “这是一个普通的Toast!”, Toast.LENGTH_SHORT);

toast.show();

设置Toast显示位置

两种方法方法可以设置显示位置:

方法一:

setGravity(int gravity, int xOffset, int yOffset)三个参数分别表示(起点位置,水平向右位移,垂直向下位移)
方法二:

setMargin(float horizontalMargin, float verticalMargin)
以横向和纵向的百分比设置显示位置,参数均为float类型(水平位移正右负左,竖直位移正上负下)

Java代码

// 设置Toast显示位置(起点位置,水平向右位移,垂直向下位移)

toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 200);

// Toast显示位置,以横向和纵向的百分比计算,参数均为float类型(水平位移正右负左,竖直位移正上负下)

toast.setMargin(-0.5f, 0f);

自定义Toast

下面这段代码可以显示一个带图片的Toast效果:

<span style="font-size:18px;">// 带图片的Toast   
        Button btn2 = (Button) findViewById(R.id.toast2);   
        btn2.setOnClickListener(new OnClickListener() {   
            public void onClick(View v) {   
                // 定义一个Toast   
Toast toast = Toast.makeText(ToastDemoActivity.this, "这是一个代图片的Toast!", Toast.LENGTH_LONG);   
               // 定义一个ImageView   
               ImageView imageView = new ImageView(ToastDemoActivity.this);   
               imageView.setImageResource(R.drawable.icon);   
               // 获得Toast的View   
               View toastView = toast.getView();   
               // 定义一个Layout,这里是Layout   
               LinearLayoutlinear Layout = new LinearLayout(ToastDemoActivity.this);   
               linearLayout.setOrientation(LinearLayout.HORIZONTAL);   
               // 将ImageView和ToastView合并到Layout中   
               linearLayout.addView(imageView);   
               linearLayout.addView(toastView);   
               // 替换掉原有的ToastView   
               toast.setView(linearLayout);   
               toast.show();   
            }   
        });</span><span style="font-size:16px;">  
</span>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现一个能够进行混合运算的Android计算器,可以按照以下步骤进行: 1. 创建一个布局文件,包含数字按钮、运算符按钮、清除按钮、等于号按钮等组件。 2. 在Java代码中,为每个按钮添加点击事件监听器,实现相应的功能。 3. 在计算器逻辑中,可以使用栈来实现混合运算。当输入一个数字时,将其压入栈中;当输入一个运算符时,从栈中弹出两个数字,进行相应的运算,并将结果压入栈中。 4. 当按下等于号按钮时,从栈中弹出最终结果,并将其显示在计算器屏幕上。 下面是一个简单的示例代码,用于实现一个简单的支持混合运算的Android计算器: MainActivity.java ```java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Stack; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView mResultTextView; private StringBuilder mInputString = new StringBuilder(); private Stack<Double> mNumberStack = new Stack<>(); private Stack<Character> mOperatorStack = new Stack<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mResultTextView = findViewById(R.id.result_text_view); Button clearButton = findViewById(R.id.clear_button); clearButton.setOnClickListener(this); Button equalsButton = findViewById(R.id.equals_button); equalsButton.setOnClickListener(this); Button addButton = findViewById(R.id.add_button); addButton.setOnClickListener(this); Button subtractButton = findViewById(R.id.subtract_button); subtractButton.setOnClickListener(this); Button multiplyButton = findViewById(R.id.multiply_button); multiplyButton.setOnClickListener(this); Button divideButton = findViewById(R.id.divide_button); divideButton.setOnClickListener(this); Button decimalButton = findViewById(R.id.decimal_button); decimalButton.setOnClickListener(this); Button zeroButton = findViewById(R.id.zero_button); zeroButton.setOnClickListener(this); Button oneButton = findViewById(R.id.one_button); oneButton.setOnClickListener(this); Button twoButton = findViewById(R.id.two_button); twoButton.setOnClickListener(this); Button threeButton = findViewById(R.id.three_button); threeButton.setOnClickListener(this); Button fourButton = findViewById(R.id.four_button); fourButton.setOnClickListener(this); Button fiveButton = findViewById(R.id.five_button); fiveButton.setOnClickListener(this); Button sixButton = findViewById(R.id.six_button); sixButton.setOnClickListener(this); Button sevenButton = findViewById(R.id.seven_button); sevenButton.setOnClickListener(this); Button eightButton = findViewById(R.id.eight_button); eightButton.setOnClickListener(this); Button nineButton = findViewById(R.id.nine_button); nineButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.clear_button: handleClearButtonClick(); break; case R.id.equals_button: handleEqualsButtonClick(); break; case R.id.add_button: case R.id.subtract_button: case R.id.multiply_button: case R.id.divide_button: handleOperatorButtonClick((Button) v); break; case R.id.decimal_button: case R.id.zero_button: case R.id.one_button: case R.id.two_button: case R.id.three_button: case R.id.four_button: case R.id.five_button: case R.id.six_button: case R.id.seven_button: case R.id.eight_button: case R.id.nine_button: handleNumberButtonClick((Button) v); break; } } private void handleClearButtonClick() { mInputString.setLength(0); mNumberStack.clear(); mOperatorStack.clear(); mResultTextView.setText(""); } private void handleEqualsButtonClick() { if (mInputString.length() == 0) { return; } double result = evaluateExpression(); mResultTextView.setText(String.valueOf(result)); } private void handleOperatorButtonClick(Button button) { if (mInputString.length() == 0) { return; } char operator = button.getText().charAt(0); while (!mOperatorStack.isEmpty() && hasPrecedence(mOperatorStack.peek(), operator)) { double rightOperand = mNumberStack.pop(); double leftOperand = mNumberStack.pop(); char op = mOperatorStack.pop(); double result = applyOperator(op, leftOperand, rightOperand); mNumberStack.push(result); } mOperatorStack.push(operator); mInputString.append(operator); mResultTextView.setText(mInputString.toString()); } private void handleNumberButtonClick(Button button) { char digit = button.getText().charAt(0); mInputString.append(digit); mResultTextView.setText(mInputString.toString()); } private double evaluateExpression() { while (!mOperatorStack.isEmpty()) { double rightOperand = mNumberStack.pop(); double leftOperand = mNumberStack.pop(); char op = mOperatorStack.pop(); double result = applyOperator(op, leftOperand, rightOperand); mNumberStack.push(result); } return mNumberStack.pop(); } private boolean hasPrecedence(char op1, char op2) { if (op2 == '(' || op2 == ')') { return false; } if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) { return false; } return true; } private double applyOperator(char op, double leftOperand, double rightOperand) { switch (op) { case '+': return leftOperand + rightOperand; case '-': return leftOperand - rightOperand; case '*': return leftOperand * rightOperand; case '/': return leftOperand / rightOperand; default: throw new IllegalArgumentException("Invalid operator: " + op); } } } ``` activity_main.xml ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/result_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:textAlignment="viewEnd" android:paddingTop="16dp" android:paddingBottom="16dp" android:paddingEnd="16dp" android:paddingStart="16dp"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/clear_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="C" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/divide_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="/" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/multiply_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="*" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/subtract_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="-" android:layout_weight="1" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/seven_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="7" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/eight_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="8" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/nine_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="9" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/add_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="+" android:layout_weight="1" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/four_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="4" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/five_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="5" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/six_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="6" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/decimal_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="." android:layout_weight="1" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/one_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="1" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/two_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="2" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/three_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="3" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/equals_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="=" android:layout_weight="1" android:textSize="18sp"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/zero_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="0" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/placeholder_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/placeholder_button_2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="" android:layout_weight="1" android:textSize="18sp"/> <Button android:id="@+id/placeholder_button_3" android:layout_width="0dp" android:layout_height="wrap_content" android:text="" android:layout_weight="1" android:textSize="18sp"/> </LinearLayout> </LinearLayout> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值