将单个TextView分解为不同的行

时间:2014-08-04 03:42:12

标签: java android

我需要将从NFC READER传输到我手机的信息安排在每行中,例如:活动ID,活动名称,日期时间,地点。

当我使用我的Android应用程序将手机点按到阅读器时,我得到的是一串文字。我想打破文本以便能够识别事件ID,EventName,DateTime,Venue能够为每个文本分别提供getText以提交给数据库。

这是我的代码

package com.techblogon.loginexample;



import java.nio.charset.Charset;
import java.util.Arrays;

import com.techblogon.loginexample.R;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.provider.Settings;
import android.text.format.Time;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class UserScreen extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback{

    private static final int MESSAGE_SENT = 1;
    NfcAdapter mNfcAdapter;
    TextView mInfoText;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userscreen);
        mInfoText = (TextView) findViewById(R.id.textView);
        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            mInfoText = (TextView) findViewById(R.id.textView);
            mInfoText.setText("NFC is not available on this device.");
        }
        // Register callback to set NDEF message
        mNfcAdapter.setNdefPushMessageCallback(this, this);
        // Register callback to listen for message-sent success
        mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
    } @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        Time time = new Time();
        time.setToNow();
        String text = ("Beam me up!\n\n" +
                "Beam Time: " + time.format("%H:%M:%S"));
        NdefMessage msg = new NdefMessage(
              new NdefRecord[] { createMimeRecord("application/com.example.android.beam", text.getBytes()) });
        return msg;
    }

    @Override
    public void onNdefPushComplete(NfcEvent arg0) {
        // A handler is needed to send messages to the activity when this
        // callback occurs, because it happens from a binder thread
        mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();        
        Log.w ("Sent",mHandler.obtainMessage(MESSAGE_SENT).toString());
    }
    /** This handler receives a message from onNdefPushComplete */
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
            case MESSAGE_SENT:
                Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
                break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        // Check to see that the Activity started due to an Android Beam
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

    // Parses the NDEF Message from the intent and prints to the TextView

   void processIntent(Intent intent) {      
       Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
               NfcAdapter.EXTRA_NDEF_MESSAGES);
       // only one message sent during the beam 
       NdefMessage msg = (NdefMessage) rawMsgs[0];
       // record 0 contains the MIME type, record 1 is the AAR, if present
       mInfoText.setText(new String(msg.getRecords()[0].getPayload())); 


   }


   public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
       byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
       NdefRecord mimeRecord = new NdefRecord(
               NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
       return mimeRecord;
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // If NFC is not available, we won't be needing this menu
       if (mNfcAdapter == null) {
           return super.onCreateOptionsMenu(menu);
       }
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.main, menu);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
           case R.id.menu_settings:
               Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
               startActivity(intent);
               return true;
           default:
               return super.onOptionsItemSelected(item);
       }
   }
    }

1 个答案:

答案 0 :(得分:0)

试试这个

textView.setText(string1+System.getProperty ("line.separator")+string2);

编辑:取两个字符串变量,如

String string1 = "Hello";
String string2 = "Android";

然后

your_textView.setText(string1 + System.getProperty ("line.separator") + string2);

OR

your_textView.setText("Hello" + System.getProperty ("line.separator") + "Android");