gpt4 book ai didi

java - 如何在toast消息中隐藏包名称?

转载 作者:行者123 更新时间:2023-12-02 02:35:58 30 4
gpt4 key购买 nike

如何去掉toast消息前的包名(Javascript)?

包名称:

com.company.javascript

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

findViewById(R.id.btn_hello).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Hello World!", Toast.LENGTH_LONG).show();
}
});
}
}

enter image description here

最佳答案

感谢@CommonsWare,这个问题是由某些设备制造商引起的,修改了Toast的行为,因此如果有必要这样做,我们必须覆盖Toast.makeText然后显示它

public class ToastUtils {

public static void showToast(Context context, String message, int duration) {
Toast toast = Toast.makeText(context, message, duration);
ViewGroup group = (ViewGroup) toast.getView();
TextView tvMessage = (TextView) group.getChildAt(0);
tvMessage.setText(message);
tvMessage.setGravity(Gravity.CENTER);
toast.show();
}
}

关于java - 如何在toast消息中隐藏包名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57202202/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com