java - 在android中显示区域字符

标签 java android char toast

我有方法:

public boolean dispatchKeyEvent(KeyEvent event) 
{
    Toast.makeText(getApplicationContext(),String.valueOf(event.getCharacters()), Toast.LENGTH_SHORT).show();
    ShowKeyboardSystem(false);
    Find(event.getCharacters());
    return true;
}

不幸的是, toast 确实显示出区域特征。为什么?

最佳答案

您可以使用为 Toast 中的文本设置的字体来显示自定义 Toast。我已经下载了 kn.ttf(卡纳达语字体,这是我居住的本地语言)并将其放入 assets 文件夹中,如下图所示。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

 </LinearLayout>

自定义 toast 布局

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/toast_layout_root"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="8dp"
          android:background="#DAAA"
          >
 <TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#FFF"
          />
 </LinearLayout>

代码:

public class MainActivity extends Activity {

private TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b= (Button) findViewById(R.id.button1);
    b.setOnClickListener( new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.custom,
                                           (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setTextColor(Color.RED);
            text.setText("This is a custom toast");
            Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/kn.ttf");
            text.setTypeface(typeFace);
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }

    });

}
}

enter image description here

关于java - 在android中显示区域字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15955872/

相关文章:

Android gradle - 如何远程访问 keystore

带有 char 和 wchar_t 的 C++ 正则表达式?

java - Hibernate 中 equals() 方法的问题

java - token 向前看的语法限制

java - 输入文本文件中的最小值、最大值和平均值

使用 putchar() 函数和指针将整数转换为字符串

c++ - 在 C 中使用 char * 时出现错误 "debug assertation failed"

java - 防御性编程 : Guidelines in Java

android - Maven android插件错误 "constant expression required"

java - Jsoup 使用什么选择器