【问题标题】:Show regional chars in android在android中显示区域字符
【发布时间】:2013-04-12 02:45:08
【问题描述】:

我有方法:

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

不幸的是,toast 确实显示了区域字符。为什么?

【问题讨论】:

  • 区域字符?你是什​​么意思? Find() 是什么?
  • 找到一种方法来搜索作为参数给出的字符串
  • 这不是我的问题的解决方案。我需要从键盘捕获区域字符并与用户之前输入的字符进行比较
  • 您的意思是非英语字符吗?回答您的问题时,Toast 显示 regional 字符,因为 event.getCharacters() 具有 regional 字符...您没有指定要做什么...

标签: java android char toast


【解决方案1】:

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

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>

吐司的自定义alyout

 <?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();
        }

    });

}
}

【讨论】:

猜你喜欢
  • 2020-03-21
  • 2015-12-20
  • 2021-08-27
  • 2013-03-01
  • 1970-01-01
  • 2014-05-20
  • 2022-01-11
  • 1970-01-01
  • 2012-09-17
相关资源
最近更新 更多