Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义Edittext中用于聊天的文本_Java_Android - Fatal编程技术网

Java 自定义Edittext中用于聊天的文本

Java 自定义Edittext中用于聊天的文本,java,android,Java,Android,我想自定义聊天应用程序edittext中的文本。我想加粗用户名和他的邮件的普通字体。 比如, usernamejay:你好,我是Jay。你好吗 在usernamejay的消息后留出1个空间进行回复。我还想更改用户名的字体颜色。如果可能的话,为每条消息放置消息气球 例如: usernamejay:你好,我是Jay。你好吗 用户名克拉克:我很好。我现在可以打电话给你开会吗 谁能帮我怎么做。这是Java的代码 import java.io.UnsupportedEncodingException; i

我想自定义聊天应用程序edittext中的文本。我想加粗用户名和他的邮件的普通字体。 比如,

usernamejay:你好,我是Jay。你好吗

在usernamejay的消息后留出1个空间进行回复。我还想更改用户名的字体颜色。如果可能的话,为每条消息放置消息气球

例如:

usernamejay:你好,我是Jay。你好吗

用户名克拉克:我很好。我现在可以打电话给你开会吗

谁能帮我怎么做。这是Java的代码

import java.io.UnsupportedEncodingException;
import com.example.healthhelpv2.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import at.vcity.androidim.interfaces.IAppManager;
import at.vcity.androidim.services.IMService;
import at.vcity.androidim.tools.FriendController;
import at.vcity.androidim.tools.LocalStorageHandler;
import at.vcity.androidim.types.FriendInfo;
import at.vcity.androidim.types.MessageInfo;


public class Messaging extends Activity {

    private static final int MESSAGE_CANNOT_BE_SENT = 0;
    public String username;
    private EditText messageText;
    private EditText messageHistoryText;
    private Button sendMessageButton;
    private IAppManager imService;
    private FriendInfo friend = new FriendInfo();
    private LocalStorageHandler localstoragehandler; 
    private Cursor dbCursor;

    private ServiceConnection mConnection = new ServiceConnection() {



        public void onServiceConnected(ComponentName className, IBinder service) {          
            imService = ((IMService.IMBinder)service).getService();
        }
        public void onServiceDisconnected(ComponentName className) {
            imService = null;
            Toast.makeText(Messaging.this, R.string.local_service_stopped,
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);    

        setContentView(R.layout.messaging_screen); //messaging_screen);

        messageHistoryText = (EditText) findViewById(R.id.messageHistory);

        messageText = (EditText) findViewById(R.id.message);

        messageText.requestFocus();         

        sendMessageButton = (Button) findViewById(R.id.sendMessageButton);

        Bundle extras = this.getIntent().getExtras();


        friend.userName = extras.getString(FriendInfo.USERNAME);
        friend.ip = extras.getString(FriendInfo.IP);
        friend.port = extras.getString(FriendInfo.PORT);
        String msg = extras.getString(MessageInfo.MESSAGETEXT);



        setTitle("Messaging with " + friend.userName);


    //  EditText friendUserName = (EditText) findViewById(R.id.friendUserName);
    //  friendUserName.setText(friend.userName);


        localstoragehandler = new LocalStorageHandler(this);
        dbCursor = localstoragehandler.get(friend.userName, IMService.USERNAME );

        if (dbCursor.getCount() > 0){
        int noOfScorer = 0;
        dbCursor.moveToFirst();
            while ((!dbCursor.isAfterLast())&&noOfScorer<dbCursor.getCount()) 
            {
                noOfScorer++;

                this.appendToMessageHistory(dbCursor.getString(2) , dbCursor.getString(3));
                dbCursor.moveToNext();
            }
        }
        localstoragehandler.close();

        if (msg != null) 
        {
            this.appendToMessageHistory(friend.userName , msg);
            ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).cancel((friend.userName+msg).hashCode());
        }

        sendMessageButton.setOnClickListener(new OnClickListener(){
            CharSequence message;
            Handler handler = new Handler();
            public void onClick(View arg0) {
                message = messageText.getText();
                if (message.length()>0) 
                {       
                    appendToMessageHistory(imService.getUsername(), message.toString());

                    localstoragehandler.insert(imService.getUsername(), friend.userName, message.toString());

                    messageText.setText("");
                    Thread thread = new Thread(){                   
                        public void run() {
                            try {
                                if (imService.sendMessage(imService.getUsername(), friend.userName, message.toString()) == null)
                                {

                                    handler.post(new Runnable(){    

                                        public void run() {

                                            Toast.makeText(getApplicationContext(),R.string.message_cannot_be_sent, Toast.LENGTH_LONG).show();


                                            //showDialog(MESSAGE_CANNOT_BE_SENT);                                       
                                        }

                                    });
                                }
                            } catch (UnsupportedEncodingException e) {
                                Toast.makeText(getApplicationContext(),R.string.message_cannot_be_sent, Toast.LENGTH_LONG).show();

                                e.printStackTrace();
                            }
                        }                       
                    };
                    thread.start();

                }

            }});

        messageText.setOnKeyListener(new OnKeyListener(){
            public boolean onKey(View v, int keyCode, KeyEvent event) 
            {
                if (keyCode == 66){
                    sendMessageButton.performClick();
                    return true;
                }
                return false;
            }


        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        int message = -1;
        switch (id)
        {
        case MESSAGE_CANNOT_BE_SENT:
            message = R.string.message_cannot_be_sent;
        break;
        }

        if (message == -1)
        {
            return null;
        }
        else
        {
            return new AlertDialog.Builder(Messaging.this)       
            .setMessage(message)
            .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    /* User clicked OK so do some stuff */
                }
            })        
            .create();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(messageReceiver);
        unbindService(mConnection);

        FriendController.setActiveFriend(null);

    }

    @Override
    protected void onResume() 
    {       
        super.onResume();
        bindService(new Intent(Messaging.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);

        IntentFilter i = new IntentFilter();
        i.addAction(IMService.TAKE_MESSAGE);

        registerReceiver(messageReceiver, i);

        FriendController.setActiveFriend(friend.userName);      


    }


    public class  MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) 
        {       
            Bundle extra = intent.getExtras();
            String username = extra.getString(MessageInfo.USERID);          
            String message = extra.getString(MessageInfo.MESSAGETEXT);

            if (username != null && message != null)
            {
                if (friend.userName.equals(username)) {
                    appendToMessageHistory(username, message);
                    localstoragehandler.insert(username,imService.getUsername(), message);

                }
                else {
                    if (message.length() > 15) {
                        message = message.substring(0, 15);
                    }
                    Toast.makeText(Messaging.this,  username + " says '"+
                                                    message + "'",
                                                    Toast.LENGTH_SHORT).show();     
                }
            }           
        }

    };
    private MessageReceiver messageReceiver = new MessageReceiver();

    public  void appendToMessageHistory(String username, String message) {
        if (username != null && message != null) {
            messageHistoryText.append(username + ":\n");                                
            messageHistoryText.append(message + "\n");
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (localstoragehandler != null) {
            localstoragehandler.close();
        }
        if (dbCursor != null) {
            dbCursor.close();
        }
    }


}
import java.io.UnsupportedEncodingException;
导入com.example.healthhelpv2.*;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Dialog;
导入android.app.NotificationManager;
导入android.content.BroadcastReceiver;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.content.ServiceConnection;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.os.Handler;
导入android.os.IBinder;
导入android.view.KeyEvent;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.view.OnKeyListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
导入at.vcity.androidim.interfaces.IAppManager;
导入at.vcity.androidim.services.IMService;
导入at.vcity.androidim.tools.FriendController;
导入at.vcity.androidim.tools.LocalStorageHandler;
导入at.vcity.androidim.types.FriendInfo;
导入at.vcity.androidim.types.MessageInfo;
公共类消息传递扩展了活动{
私有静态最终整型消息\u无法\u发送=0;
公共字符串用户名;
私有编辑文本消息文本;
私有编辑文本消息历史文本;
私人按钮SendMessage按钮;
私人IAppManager imService;
private FriendInfo friend=新建FriendInfo();
私有LocalStorageHandler LocalStorageHandler;
私有游标dbCursor;
专用ServiceConnection mConnection=新ServiceConnection(){
服务连接上的公共无效(组件名称类名称,IBinder服务){
imService=((imService.IMBinder)service.getService();
}
ServiceDisconnected上的公共void(ComponentName类名称){
imService=null;
Toast.makeText(Messaging.this,R.string.local_服务已停止,
吐司。长度(短)。show();
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.messaging_屏幕);//messaging_屏幕);
messageHistoryText=(EditText)findViewById(R.id.messageHistory);
messageText=(EditText)findViewById(R.id.message);
messageText.requestFocus();
sendMessageButton=(按钮)findViewById(R.id.sendMessageButton);
Bundle extras=this.getIntent().getExtras();
friend.userName=extras.getString(FriendInfo.userName);
friend.ip=extras.getString(FriendInfo.ip);
friend.port=extras.getString(FriendInfo.port);
String msg=extras.getString(MessageInfo.MESSAGETEXT);
setTitle(“与“+朋友.用户名”的消息传递);
//EditText-friendUserName=(EditText)findViewById(R.id.friendUserName);
//friendUserName.setText(friend.userName);
localstoragehandler=新的localstoragehandler(此);
dbCursor=localstoragehandler.get(friend.userName,IMService.userName);
if(dbCursor.getCount()>0){
int noOfScorer=0;
dbCursor.moveToFirst();
而((!dbCursor.isAfterLast())和&noOfScorer0)
{       
appendToMessageHistory(imService.getUsername(),message.toString());
localstoragehandler.insert(imService.getUsername()、friend.userName、message.toString());
messageText.setText(“”);
Thread Thread=新线程(){
公开募捐{
试一试{
if(imService.sendMessage(imService.getUsername()、friend.userName、message.toString())==null)
{
handler.post(新的Runnable(){
公开募捐{
Toast.makeText(getApplicationContext(),R.string.message_无法发送,Toast.LENGTH_LONG).show();
//showDialog(无法发送消息);
}
});
}
}捕获(不支持的编码异常e){
Toast.makeText(getApplicationContext(),R.string.message_无法发送,Toast.LENGTH_LONG).show();
e、 printStackTrace();
}
}                       
};
thread.start();
}
}});
messageText.setOnKeyListener(新的OnKeyListener(){
公共布尔onKey(视图v、int keyCode、KeyEvent事件)
{
如果(键代码==66){
sendMessageButton.performClick();
返回true;
}
返回false;
}
});
}
@凌驾
受保护的对话框onCreateDialog(int id){
int message=-1;
开关(id)
{
无法发送案例消息:
message=R.string.message无法发送;
溴
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ccbfbf"
    android:orientation="vertical"
    android:padding="10dip" >

<!--    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="Friend:"
    />

    <EditText android:id="@+id/friendUserName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:singleLine="true"
        android:editable="false" />
--> 
    <TextView   android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dip"
                android:text="Messages:"/>

    <EditText   android:id="@+id/messageHistory"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:clickable="true"    
                android:layout_weight="1"
                android:editable="false"
                android:gravity="top"
                android:scrollbars="vertical"
                android:scrollbarSize="10px"
                /> 

    <LinearLayout android:orientation="horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="4">

            <EditText
                android:id="@+id/message"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="top"
                android:hint="Type message here!" />

            <Button android:id="@+id/sendMessageButton"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="4"
                    android:text="Send"/>

    </LinearLayout>

</LinearLayout>
EditText editText = (EditText)findViewById(R.id.editText);
        String userName = "usernamejay";
        String message = "Hi This is Jay. How are you?";
        String finalStr = "<b>"+userName+":</b> "+message+"";
        editText.setText(Html.fromHtml(finalStr));
 final SpannableString out0 = new SpannableString(source[position]);
 StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
 out0.setSpan(boldSpan, 6, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 holder.tv.setText(out0);